package cli

import (
	"fmt"
	"os"

	"coopcloud.tech/abra/cli/app"
	"coopcloud.tech/abra/cli/internal"
	"coopcloud.tech/abra/cli/recipe"
	"coopcloud.tech/abra/cli/server"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli/v2"
)

// Verbose stores the variable from VerboseFlag
var Verbose bool

// VerboseFlag turns on/off verbose logging down to the INFO level
var VerboseFlag = &cli.BoolFlag{
	Name:        "verbose",
	Aliases:     []string{"V"},
	Value:       false,
	Destination: &Verbose,
	Usage:       "Show INFO messages",
}

// Debug stores the variable from DebugFlag
var Debug bool

// DebugFlag turns on/off verbose logging down to the DEBUG level
var DebugFlag = &cli.BoolFlag{
	Name:        "debug",
	Aliases:     []string{"d"},
	Value:       false,
	Destination: &Debug,
	Usage:       "Show DEBUG messages",
}

// NoPrompt stores the variable from NoPromptFlag
var NoPrompt bool

// NoPromptFlag turns on/off non-interactive mode where no user input is required
var NoPromptFlag = &cli.BoolFlag{
	Name:        "no-prompt",
	Aliases:     []string{"n"},
	Value:       false,
	Destination: &NoPrompt,
	Usage:       "Don't prompt for input and run non-interactively",
}

// Env stores the variable from EnvFlag
var Env string

// EnvFlag takes a path to an env file to load variables from for the abra cli cmd
var EnvFlag = &cli.PathFlag{
	Name:        "env",
	Aliases:     []string{"e"},
	Value:       "",
	Destination: &Env,
	Usage:       "Environment variables to load",
}

// Branch stores the variable from BranchFlag
var Branch string

// BranchFlag takes the name of the git branch to use in app cloning
var BranchFlag = &cli.StringFlag{
	Name:        "branch",
	Aliases:     []string{"b"},
	Value:       "",
	Destination: &Branch,
	Usage:       "Git branch to use while cloning app repos",
}

// SkipUpdate stores the variable from SkipUpdateFlag
var SkipUpdate bool

// SkipUpdateFlag allows users to skip updating recipe definitions
var SkipUpdateFlag = &cli.BoolFlag{
	Name:        "skip-update",
	Aliases:     []string{"U"},
	Value:       false,
	Destination: &SkipUpdate,
	Usage:       "Don't pull latest app definitions",
}

// SkipCheck stores the variable from SkipCheckFlag
var SkipCheck bool

// SkipCheckFlag allows users to skip checking app vars
var SkipCheckFlag = &cli.BoolFlag{
	Name:        "skip-check",
	Aliases:     []string{"C"},
	Value:       false,
	Destination: &SkipCheck,
	Usage:       "Don't verify app variables",
}

// Stack stores the variable from StackFlag
var Stack string

// StackFlag gets the name of the target stack to run commands against
var StackFlag = &cli.StringFlag{
	Name:        "stack",
	Aliases:     []string{"s"},
	Value:       "",
	Destination: &Stack,
	Usage:       "Name of the target stack",
}

// RunApp runs CLI abra app
func RunApp(version, commit string) {
	app := &cli.App{
		Name: "abra",
		Usage: `The Co-op Cloud command-line utility belt 🎩🐇

    ____                           ____ _                 _
   / ___|___         ___  _ __    / ___| | ___  _   _  __| |
  | |   / _ \ _____ / _ \| '_ \  | |   | |/ _ \| | | |/ _' |
  | |__| (_) |_____| (_) | |_) | | |___| | (_) | |_| | (_| |
   \____\___/       \___/| .__/   \____|_|\___/ \__,_|\__,_|
                         |_|
		`,
		Version: fmt.Sprintf("%s-%s", version, commit[:7]),
		Commands: []*cli.Command{
			app.AppCommand,
			server.ServerCommand,
			recipe.RecipeCommand,
			VersionCommand,
		},
		Flags: []cli.Flag{
			EnvFlag,
			StackFlag,
			SkipCheckFlag,
			SkipUpdateFlag,
			VerboseFlag,
			BranchFlag,
			NoPromptFlag,
			DebugFlag,
			internal.ContextFlag,
		},
	}

	err := app.Run(os.Args)
	if err != nil {
		logrus.Fatal(err)
	}
}