package cmd

import (
	"os"

	"github.com/spf13/cobra"
)

var (
	cfgFile string
	dataDir string
)

// rootCmd is the single instance of the root command
var rootCmd = &cobra.Command{
	Use:   "ums",
	Short: "UMS CLI - User Management System",
	Long: `UMS CLI - Command line interface for User Management System

Supported commands:
  ums init     Initialize the system (interactive or with flags)
  ums serve    Start the UMS server
  ums version  Print version information`,
}

func init() {
	cobra.OnInitialize(func() {
		_ = os.Setenv("CONFIG_FILE", cfgFile)
		_ = os.Setenv("DATA_DIR", dataDir)
	})

	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "./config.yaml", "config file path")
	rootCmd.PersistentFlags().StringVar(&dataDir, "data-dir", "./data", "data directory")

	rootCmd.AddCommand(
		newVersionCmd(),
		initCmd,
		serveCmd,
	)
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		os.Exit(1)
	}
}
