package main

import (
	"kode-starter/cmdCreate"
	"kode-starter/cmdEnv"
	"kode-starter/cmdLists"
	"kode-starter/config"
	"kode-starter/utils"
	"kode-starter/version"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "kode-creator.exe",
	Short: "Simple cli app create startup project",
	Long:  `A simple CLI app to work with Github, Gitea`,
	Run: func(cmd *cobra.Command, args []string) {

		config.SetInformations(cmd, args)
		mainProgram(cmd, args)
	},
}

var token, org, name, private, description, group string
var verbose, createFlag bool
var listFlag, envVariablesFlag, groupFlag bool

func init() {

	rootCmd.Flags().BoolP("help", "h", false, "Show help for create command")

	rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "mode verbose")
	rootCmd.PersistentFlags().BoolVarP(&createFlag, "json", "j", false, "Print output as json format")
	// rootCmd.PersistentFlags().BoolVarP(&envVariablesFlag, "env", "e", false, "environment variables ")

	cmdLists.ListCmd.Flags().StringVarP(&token, "token", "t", "", "Github token")
	cmdLists.ListCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization")

	cmdCreate.CreateCmd.Flags().StringVarP(&token, "token", "t", "", "Github token")
	cmdCreate.CreateCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization")
	cmdCreate.CreateCmd.Flags().StringVarP(&name, "name", "n", "", "Project name")
	cmdCreate.CreateCmd.Flags().StringVarP(&description, "desc", "d", "", "Description")
	cmdCreate.CreateCmd.Flags().StringVarP(&private, "private", "p", "", "true/false")

	cmdEnv.EnvCmd.Flags().BoolVarP(&listFlag, "list", "l", false, "List options")
	cmdEnv.EnvCmd.Flags().BoolVarP(&envVariablesFlag, "env", "e", false, "environnement variables")
	cmdEnv.EnvCmd.Flags().BoolVarP(&groupFlag, "group", "g", false, "categories of informations")
	cmdEnv.EnvCmd.Flags().StringVarP(&group, "filter", "f", "", "filter for a specific category")

	rootCmd.AddCommand(cmdLists.ListCmd)
	rootCmd.AddCommand(cmdCreate.CreateCmd)
	rootCmd.AddCommand(cmdEnv.EnvCmd)

}

func mainProgram(cmd *cobra.Command, args []string) {

	config.SetInformations(cmd, args)

	utils.PrintHeader()

	// Check if the help flag is set
	if cmd.Flags().Changed("help") || cmd.Flags().Changed("h") {
		utils.PrintHelpFormated(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate(), cmd)
		return
	} else {

		// If no flag is set, show help
		utils.PrintHelpFormated(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate(), cmd)
		return

	}

}

func main() {

	rootCmd.Execute()

}
