package cmd

import (
	"fmt"
	"os"
)

// RunUpdate handles the "update" command
func RunUpdate(args []string) {
	if len(args) < 1 {
		printUpdateUsage()
		os.Exit(1)
	}

	switch args[0] {
	case "dataset":
		RunDatasetUpdate(args[1:])
	case "location":
		RunLocationUpdate(args[1:])
	case "cluster":
		RunClusterUpdate(args[1:])
	case "pattern":
		RunPatternUpdate(args[1:])
	default:
		fmt.Fprintf(os.Stderr, "Unknown resource to update: %s\n", args[0])
		printUpdateUsage()
		os.Exit(1)
	}
}

func printUpdateUsage() {
	fmt.Fprintf(os.Stderr, "Usage: skraak update <resource> [options]\n\n")
	fmt.Fprintf(os.Stderr, "Resources:\n")
	fmt.Fprintf(os.Stderr, "  dataset    Update an existing dataset\n")
	fmt.Fprintf(os.Stderr, "  location   Update an existing location\n")
	fmt.Fprintf(os.Stderr, "  cluster    Update an existing cluster\n")
	fmt.Fprintf(os.Stderr, "  pattern    Update an existing pattern\n")
	fmt.Fprintf(os.Stderr, "\nExamples:\n")
	fmt.Fprintf(os.Stderr, "  skraak update dataset --db ./db/skraak.duckdb --id abc123 --name \"Updated Name\"\n")
	fmt.Fprintf(os.Stderr, "  skraak update location --db ./db/skraak.duckdb --id loc123 --name \"New Name\" --lat -36.85 --lon 174.76\n")
	fmt.Fprintf(os.Stderr, "  skraak update cluster --db ./db/skraak.duckdb --id clust123 --name \"New Name\" --sample-rate 192000\n")
	fmt.Fprintf(os.Stderr, "  skraak update pattern --db ./db/skraak.duckdb --id pattern123 --name \"New Name\" --start-time 19:00 --end-time 05:00\n")
}