package main

import (
	"context"
	"fmt"

	"dance-lessons-coach/pkg/config"
	"dance-lessons-coach/pkg/server"
	"dance-lessons-coach/pkg/version"

	"github.com/rs/zerolog/log"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "dance-lessons-coach",
	Short: "dance-lessons-coach - API server and CLI tools",
	Long: `dance-lessons-coach provides greeting services and API management.

To begin working with dance-lessons-coach, run:
  dance-lessons-coach server --help`,
	SilenceUsage: true,
}

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print version information",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(version.Full())
	},
}

var serverCmd = &cobra.Command{
	Use:   "server",
	Short: "Start the dance-lessons-coach server",
	Run: func(cmd *cobra.Command, args []string) {
		// Load config and start server
		cfg, err := config.LoadConfig()
		if err != nil {
			log.Fatal().Err(err).Msg("Failed to load configuration")
		}
		server := server.NewServer(cfg, context.Background())
		if err := server.Run(); err != nil {
			log.Fatal().Err(err).Msg("Server failed")
		}
	},
}

var greetCmd = &cobra.Command{
	Use:   "greet [name]",
	Short: "Greet someone by name",
	Args:  cobra.MaximumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		name := ""
		if len(args) > 0 {
			name = args[0]
		}
		fmt.Printf("Hello %s!\n", name)
	},
}

func init() {
	rootCmd.AddCommand(versionCmd)
	rootCmd.AddCommand(serverCmd)
	rootCmd.AddCommand(greetCmd)

	// Add flags to server command
	serverCmd.Flags().String("config", "", "Config file path")
	serverCmd.Flags().String("env", "", "Environment (dev, staging, prod)")
	serverCmd.Flags().Bool("debug", false, "Enable debug logging")
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		log.Fatal().Err(err).Msg("CLI execution failed")
	}
}
