package cmd

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/spf13/cobra"
	"go.uber.org/zap"

	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/config"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/repo/collection"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/pkg/storage/database/cassandradb"
)

var recalculateFileCountsCmd = &cobra.Command{
	Use:   "recalculate-file-counts",
	Short: "Recalculate file counts for all collections",
	Long: `Recalculates the file_count field for all collections by counting
the actual number of active files in each collection.

This command is useful for:
- Fixing collections created before file count tracking was implemented
- Repairing file counts that may have become out of sync
- Data migration and maintenance tasks

Example:
  maplefile-backend recalculate-file-counts`,
	Run: runRecalculateFileCounts,
}

func init() {
	rootCmd.AddCommand(recalculateFileCountsCmd)
}

func runRecalculateFileCounts(cmd *cobra.Command, args []string) {
	fmt.Println("🔧 Recalculating file counts for all collections...")

	// Load configuration
	cfg, err := config.Load()
	if err != nil {
		log.Fatalf("Failed to load configuration: %v", err)
	}

	// Create logger
	logger, err := zap.NewProduction()
	if err != nil {
		log.Fatalf("Failed to create logger: %v", err)
	}
	defer logger.Sync()

	// Connect to Cassandra
	fmt.Println("📦 Connecting to database...")
	session, err := cassandradb.NewCassandraConnection(cfg, logger)
	if err != nil {
		log.Fatalf("Failed to connect to Cassandra: %v", err)
	}
	defer session.Close()

	// Create collection repository
	collectionRepo := collection.NewRepository(cfg, session, logger)

	// Create context with timeout
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
	defer cancel()

	// Run recalculation
	fmt.Println("🔄 Starting recalculation...")
	startTime := time.Now()

	result, err := collectionRepo.RecalculateAllFileCounts(ctx)
	if err != nil {
		log.Fatalf("Failed to recalculate file counts: %v", err)
	}

	duration := time.Since(startTime)

	// Print results
	fmt.Println("")
	fmt.Println("✅ Recalculation completed!")
	fmt.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
	fmt.Printf("   Total collections: %d\n", result.TotalCollections)
	fmt.Printf("   Updated:           %d\n", result.UpdatedCount)
	fmt.Printf("   Errors:            %d\n", result.ErrorCount)
	fmt.Printf("   Duration:          %s\n", duration.Round(time.Millisecond))
	fmt.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")

	if result.ErrorCount > 0 {
		fmt.Println("⚠️  Some collections had errors. Check the logs for details.")
	}
}
