package cmd

import (
	"context"
	"flag"
	"fmt"
	"os"
	"strconv"

	"skraak/db"
	"skraak/tools"
)

// RunRecordingCluster handles the "cluster" subcommand (for database cluster records)
func RunRecordingCluster(args []string) {
	if len(args) < 1 {
		printRecordingClusterUsage()
		os.Exit(1)
	}

	switch args[0] {
	case "create":
		RunClusterCreate(args[1:])
	case "update":
		RunClusterUpdate(args[1:])
	default:
		fmt.Fprintf(os.Stderr, "Unknown cluster subcommand: %s\n\n", args[0])
		printRecordingClusterUsage()
		os.Exit(1)
	}
}

func printRecordingClusterUsage() {
	fmt.Fprintf(os.Stderr, "Usage: skraak cluster <subcommand> [options]\n\n")
	fmt.Fprintf(os.Stderr, "Subcommands:\n")
	fmt.Fprintf(os.Stderr, "  create    Create a new cluster\n")
	fmt.Fprintf(os.Stderr, "  update    Update an existing cluster\n")
	fmt.Fprintf(os.Stderr, "\nExamples:\n")
	fmt.Fprintf(os.Stderr, "  skraak cluster create --db ./db/skraak.duckdb --dataset abc123 --location loc456 --name \"2024-01\" --sample-rate 250000\n")
	fmt.Fprintf(os.Stderr, "  skraak cluster update --db ./db/skraak.duckdb --id clust123 --name \"New Name\"\n")
}

func RunClusterCreate(args []string) {
	fs := flag.NewFlagSet("cluster create", flag.ExitOnError)
	dbPath := fs.String("db", "", "Path to DuckDB database (required)")
	datasetID := fs.String("dataset", "", "Dataset ID (required)")
	locationID := fs.String("location", "", "Location ID (required)")
	name := fs.String("name", "", "Cluster name (required)")
	sampleRate := fs.String("sample-rate", "", "Sample rate in Hz (required)")
	description := fs.String("description", "", "Cluster description (optional)")

	fs.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: skraak cluster create [options]\n\n")
		fmt.Fprintf(os.Stderr, "Create a new cluster for grouping recordings.\n\n")
		fmt.Fprintf(os.Stderr, "Options:\n")
		fs.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\nExamples:\n")
		fmt.Fprintf(os.Stderr, "  skraak cluster create --db ./db/skraak.duckdb --dataset abc123 --location loc456 --name \"2024-01\" --sample-rate 250000\n")
	}

	if err := fs.Parse(args); err != nil {
		os.Exit(1)
	}

	// Validate required flags
	missing := []string{}
	if *dbPath == "" {
		missing = append(missing, "--db")
	}
	if *datasetID == "" {
		missing = append(missing, "--dataset")
	}
	if *locationID == "" {
		missing = append(missing, "--location")
	}
	if *name == "" {
		missing = append(missing, "--name")
	}
	if *sampleRate == "" {
		missing = append(missing, "--sample-rate")
	}
	if len(missing) > 0 {
		fmt.Fprintf(os.Stderr, "Error: missing required flags: %v\n\n", missing)
		fs.Usage()
		os.Exit(1)
	}

	// Parse sample rate
	sr, err := strconv.Atoi(*sampleRate)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: invalid sample rate: %v\n", err)
		os.Exit(1)
	}

	tools.SetDBPath(*dbPath)

	// Initialize event log
	eventLogPath := *dbPath + ".events.jsonl"
	db.SetEventLogConfig(db.EventLogConfig{
		Enabled: true,
		Path:    eventLogPath,
	})
	defer db.CloseEventLog()

	input := tools.ClusterInput{
		DatasetID:   datasetID,
		LocationID:  locationID,
		Name:        name,
		SampleRate:  &sr,
		Description: description,
	}

	output, err := tools.CreateOrUpdateCluster(context.Background(), input)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}

	printJSON(output)
}

func RunClusterUpdate(args []string) {
	fs := flag.NewFlagSet("cluster update", flag.ExitOnError)
	dbPath := fs.String("db", "", "Path to DuckDB database (required)")
	id := fs.String("id", "", "Cluster ID (required)")
	name := fs.String("name", "", "New cluster name (optional)")
	sampleRate := fs.String("sample-rate", "", "New sample rate in Hz (optional)")
	description := fs.String("description", "", "New cluster description (optional)")

	fs.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: skraak cluster update [options]\n\n")
		fmt.Fprintf(os.Stderr, "Update an existing cluster. Only provided fields are updated.\n\n")
		fmt.Fprintf(os.Stderr, "Options:\n")
		fs.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\nExamples:\n")
		fmt.Fprintf(os.Stderr, "  skraak cluster update --db ./db/skraak.duckdb --id clust123 --name \"New Name\"\n")
	}

	if err := fs.Parse(args); err != nil {
		os.Exit(1)
	}

	// Validate required flags
	missing := []string{}
	if *dbPath == "" {
		missing = append(missing, "--db")
	}
	if *id == "" {
		missing = append(missing, "--id")
	}
	if len(missing) > 0 {
		fmt.Fprintf(os.Stderr, "Error: missing required flags: %v\n\n", missing)
		fs.Usage()
		os.Exit(1)
	}

	// Parse optional sample rate
	var sr *int
	if *sampleRate != "" {
		srVal, err := strconv.Atoi(*sampleRate)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error: invalid sample rate: %v\n", err)
			os.Exit(1)
		}
		sr = &srVal
	}

	tools.SetDBPath(*dbPath)

	// Initialize event log
	eventLogPath := *dbPath + ".events.jsonl"
	db.SetEventLogConfig(db.EventLogConfig{
		Enabled: true,
		Path:    eventLogPath,
	})
	defer db.CloseEventLog()

	// Build input - only set fields that were provided (non-empty)
	input := tools.ClusterInput{
		ID: id,
	}
	if *name != "" {
		input.Name = name
	}
	if sr != nil {
		input.SampleRate = sr
	}
	if *description != "" {
		input.Description = description
	}

	output, err := tools.CreateOrUpdateCluster(context.Background(), input)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		os.Exit(1)
	}

	printJSON(output)
}