package cmds

import (
	"log"
	"os"

	"git.ma-al.com/goc_daniel/b2b/app/delivery/web"
	"git.ma-al.com/goc_daniel/b2b/app/service/langsService"
	"git.ma-al.com/goc_daniel/b2b/app/utils/version"
	"github.com/spf13/cobra"
)

var (
	rootCmd = &cobra.Command{
		Use:   "b2b",
		Short: "This is set of tools for b2b application",
		Run: func(cmd *cobra.Command, args []string) {

			if versionFlag, _ := cmd.Flags().GetBool("version"); versionFlag {
				log.Println(version.String())
				return
			}

			// Create and setup the server
			server := web.New()

			// Configure routes
			if err := server.Setup(); err != nil {
				log.Fatalf("Failed to setup server: %v", err)
			}

			// Load translations on startup
			if err := langsService.LangSrv.LoadTranslations(); err != nil {
				log.Printf("Warning: Failed to load translations on startup: %v", err)
			} else {
				log.Println("Translations loaded successfully on startup")
			}

			// Start the server
			if err := server.Run(); err != nil {
				log.Fatalf("Failed to start server: %v", err)
			}

		},
	}
)

func Execute() error {

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

func init() {
	rootCmd.Flags().BoolP("version", "v", false, "show version and exit")
	rootCmd.AddCommand(generateModelsCmd)
}
