package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"time"

	"security-center-backend/internal/modules/caasm/collector/aws"
)

func main() {
	// 1. Get Credentials from Env
	accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
	secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
	region := os.Getenv("AWS_REGION")

	if accessKey == "" || secretKey == "" || region == "" {
		fmt.Println("Error: Please set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION environment variables.")
		os.Exit(1)
	}

	fmt.Printf("Initializing AWS Collector for region %s...\n", region)

	// 2. Initialize Collector
	// We pass 0 as cloudAccountID for testing
	col, err := aws.NewAWSCollector(context.Background(), accessKey, secretKey, region, 0, nil)
	if err != nil {
		fmt.Printf("Failed to create collector: %v\n", err)
		os.Exit(1)
	}

	// 3. Run Collection
	fmt.Println("Starting collection (this may take a moment)...")
	start := time.Now()
	result, err := col.Collect(context.Background())
	if err != nil {
		fmt.Printf("Collection failed: %v\n", err)
		os.Exit(1)
	}
	duration := time.Since(start)

	// 4. Print Results
	fmt.Printf("\n--- Collection Success (took %v) ---\n", duration)
	fmt.Printf("Total Assets: %d\n", len(result.Assets))
	fmt.Printf("Total IP Assets: %d\n", len(result.IPAssets))
	fmt.Printf("Total Domain Assets: %d\n", len(result.DomainAssets))

	// Group by Type
	byType := make(map[string]int)
	for _, a := range result.Assets {
		byType[string(a.AssetType)]++
	}
	fmt.Println("\nAssets by Type:")
	for t, count := range byType {
		fmt.Printf("- %s: %d\n", t, count)
	}

	// Print first 5 assets as sample
	fmt.Println("\nSample Assets:")
	for i, a := range result.Assets {
		if i >= 5 {
			break
		}
		// Pretty print metadata
		metaBytes, _ := json.MarshalIndent(a.Metadata, "  ", "  ")
		fmt.Printf("[%s] %s (ID: %s)\n  Metadata: %s\n", a.AssetType, a.Name, a.NativeID, string(metaBytes))
	}
}
