package internal

import (
	"errors"
	"strings"

	"coopcloud.tech/abra/pkg/app"
	"coopcloud.tech/abra/pkg/catalogue"
	"coopcloud.tech/abra/pkg/config"
	"coopcloud.tech/abra/pkg/recipe"
	"coopcloud.tech/abra/pkg/ssh"
	"github.com/AlecAivazis/survey/v2"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli/v2"
)

// AppName is used for configuring app name programmatically
var AppName string

// ValidateRecipe ensures the recipe arg is valid.
func ValidateRecipe(c *cli.Context) recipe.Recipe {
	recipeName := c.Args().First()

	if recipeName == "" {
		ShowSubcommandHelpAndError(c, errors.New("no recipe provided"))
	}

	recipe, err := recipe.Get(recipeName)
	if err != nil {
		if c.Command.Name == "generate" {
			if strings.Contains(err.Error(), "missing a compose") {
				logrus.Fatal(err)
			}
			logrus.Warn(err)
		} else {
			logrus.Fatal(err)
		}
	}

	logrus.Debugf("validated '%s' as recipe argument", recipeName)

	return recipe
}

// ValidateRecipeWithPrompt ensures a recipe argument is present before
// validating, asking for input if required.
func ValidateRecipeWithPrompt(c *cli.Context) recipe.Recipe {
	recipeName := c.Args().First()

	if recipeName == "" && !NoInput {
		var recipes []string

		catl, err := catalogue.ReadRecipeCatalogue()
		if err != nil {
			logrus.Fatal(err)
		}

		knownRecipes := make(map[string]bool)
		for name := range catl {
			knownRecipes[name] = true
		}

		localRecipes, err := recipe.GetRecipesLocal()
		if err != nil {
			logrus.Fatal(err)
		}

		for _, recipeLocal := range localRecipes {
			if _, ok := knownRecipes[recipeLocal]; !ok {
				knownRecipes[recipeLocal] = true
			}
		}

		for recipeName := range knownRecipes {
			recipes = append(recipes, recipeName)
		}

		prompt := &survey.Select{
			Message: "Select recipe",
			Options: recipes,
		}
		if err := survey.AskOne(prompt, &recipeName); err != nil {
			logrus.Fatal(err)
		}
	}

	if RecipeName != "" {
		recipeName = RecipeName
		logrus.Debugf("programmatically setting recipe name to %s", recipeName)
	}

	if recipeName == "" {
		ShowSubcommandHelpAndError(c, errors.New("no recipe provided"))
	}

	recipe, err := recipe.Get(recipeName)
	if err != nil {
		logrus.Fatal(err)
	}

	logrus.Debugf("validated %s as recipe argument", recipeName)

	return recipe
}

// ValidateApp ensures the app name arg is valid.
func ValidateApp(c *cli.Context) config.App {
	appName := c.Args().First()

	if AppName != "" {
		appName = AppName
		logrus.Debugf("programmatically setting app name to %s", appName)
	}

	if appName == "" {
		ShowSubcommandHelpAndError(c, errors.New("no app provided"))
	}

	app, err := app.Get(appName)
	if err != nil {
		logrus.Fatal(err)
	}

	if err := recipe.EnsureExists(app.Type); err != nil {
		logrus.Fatal(err)
	}

	if err := ssh.EnsureHostKey(app.Server); err != nil {
		logrus.Fatal(err)
	}

	logrus.Debugf("validated %s as app argument", appName)

	return app
}

// ValidateDomain ensures the domain name arg is valid.
func ValidateDomain(c *cli.Context) (string, error) {
	domainName := c.Args().First()

	if domainName == "" && !NoInput {
		prompt := &survey.Input{
			Message: "Specify a domain name",
			Default: "example.com",
		}
		if err := survey.AskOne(prompt, &domainName); err != nil {
			return domainName, err
		}
	}

	if domainName == "" {
		ShowSubcommandHelpAndError(c, errors.New("no domain provided"))
	}

	logrus.Debugf("validated %s as domain argument", domainName)

	return domainName, nil
}

// ValidateSubCmdFlags ensures flag order conforms to correct order
func ValidateSubCmdFlags(c *cli.Context) bool {
	for argIdx, arg := range c.Args().Slice() {
		if !strings.HasPrefix(arg, "--") {
			for _, flag := range c.Args().Slice()[argIdx:] {
				if strings.HasPrefix(flag, "--") {
					return false
				}
			}
		}
	}
	return true
}

// ValidateServer ensures the server name arg is valid.
func ValidateServer(c *cli.Context) (string, error) {
	serverName := c.Args().First()

	serverNames, err := config.ReadServerNames()
	if err != nil {
		return serverName, err
	}

	if serverName == "" && !NoInput {
		prompt := &survey.Select{
			Message: "Specify a server name",
			Options: serverNames,
		}
		if err := survey.AskOne(prompt, &serverName); err != nil {
			return serverName, err
		}
	}

	if serverName == "" {
		ShowSubcommandHelpAndError(c, errors.New("no server provided"))
	}

	logrus.Debugf("validated %s as server argument", serverName)

	return serverName, nil
}