package app

import (
	"context"
	"fmt"

	"coopcloud.tech/abra/cli/internal"
	"coopcloud.tech/abra/pkg/app"
	"coopcloud.tech/abra/pkg/autocomplete"
	"coopcloud.tech/abra/pkg/config"
	"coopcloud.tech/abra/pkg/envfile"
	"coopcloud.tech/abra/pkg/secret"

	appPkg "coopcloud.tech/abra/pkg/app"
	"coopcloud.tech/abra/pkg/client"
	"coopcloud.tech/abra/pkg/dns"
	"coopcloud.tech/abra/pkg/formatter"
	"coopcloud.tech/abra/pkg/lint"
	"coopcloud.tech/abra/pkg/log"
	"coopcloud.tech/abra/pkg/upstream/stack"
	dockerClient "github.com/docker/docker/client"
	"github.com/spf13/cobra"
)

var AppDeployCommand = &cobra.Command{
	Use:     "deploy <domain> [version] [flags]",
	Aliases: []string{"d"},
	Short:   "Deploy an app",
	Long: `Deploy an app.

This command supports chaos operations. Use "--chaos/-C" to deploy your recipe
checkout as-is. Recipe commit hashes are also supported as values for
"[version]". Please note, "upgrade"/"rollback" do not support chaos operations.`,
	Example: `  # standard deployment
  abra app deploy 1312.net

  # chaos deployment
  abra app deploy 1312.net --chaos
  
  # deploy specific version
  abra app deploy 1312.net 2.0.0+1.2.3

  # deploy a specific git hash
  abra app deploy 1312.net 886db76d`,
	Args: cobra.RangeArgs(1, 2),
	ValidArgsFunction: func(
		cmd *cobra.Command,
		args []string,
		toComplete string) ([]string, cobra.ShellCompDirective) {
		switch l := len(args); l {
		case 0:
			return autocomplete.AppNameComplete()
		case 1:
			app, err := appPkg.Get(args[0])
			if err != nil {
				errMsg := fmt.Sprintf("autocomplete failed: %s", err)
				return []string{errMsg}, cobra.ShellCompDirectiveError
			}
			return autocomplete.RecipeVersionComplete(app.Recipe.Name)
		default:
			return nil, cobra.ShellCompDirectiveDefault
		}
	},
	Run: func(cmd *cobra.Command, args []string) {
		var (
			deployWarnMessages   []string
			toDeployVersion      string
			isChaosCommit        bool
			toDeployChaosVersion = config.CHAOS_DEFAULT
		)

		app := internal.ValidateApp(args)

		if err := validateArgsAndFlags(args); err != nil {
			log.Fatal(err)
		}

		if err := app.Recipe.Ensure(internal.GetEnsureContext()); err != nil {
			log.Fatal(err)
		}

		if err := lint.LintForErrors(app.Recipe); err != nil {
			log.Fatal(err)
		}

		cl, err := client.New(app.Server)
		if err != nil {
			log.Fatal(err)
		}

		log.Debugf("checking whether %s is already deployed", app.StackName())

		deployMeta, err := stack.IsDeployed(context.Background(), cl, app.StackName())
		if err != nil {
			log.Fatal(err)
		}

		if deployMeta.IsDeployed && !(internal.Force || internal.Chaos) {
			log.Fatalf("%s is already deployed", app.Name)
		}

		if len(args) == 2 && args[1] != "" {
			toDeployVersion = args[1]
		}

		if !deployMeta.IsDeployed &&
			toDeployVersion == "" &&
			app.Recipe.EnvVersion != "" && !internal.IgnoreEnvVersion {
			log.Debugf("new deployment, choosing .env version: %s", app.Recipe.EnvVersion)
			toDeployVersion = app.Recipe.EnvVersion
		}

		if !internal.Chaos && toDeployVersion == "" {
			if err := getLatestVersionOrCommit(app, &toDeployVersion); err != nil {
				log.Fatal(err)
			}
		}

		if internal.Chaos {
			if err := getChaosVersion(app, &toDeployVersion, &toDeployChaosVersion); err != nil {
				log.Fatal(err)
			}
		}

		if !internal.Chaos {
			isChaosCommit, err = app.Recipe.EnsureVersion(toDeployVersion)
			if err != nil {
				log.Fatal(err)
			}

			if isChaosCommit {
				log.Debugf("assuming chaos commit: %s", toDeployVersion)

				internal.Chaos = true
				toDeployChaosVersion = toDeployVersion

				toDeployVersion, err = app.Recipe.GetVersionLabelLocal()
				if err != nil {
					log.Fatal(err)
				}
			}
		}

		if err := validateSecrets(cl, app); err != nil {
			log.Fatal(err)
		}

		abraShEnv, err := envfile.ReadAbraShEnvVars(app.Recipe.AbraShPath)
		if err != nil {
			log.Fatal(err)
		}
		for k, v := range abraShEnv {
			app.Env[k] = v
		}

		composeFiles, err := app.Recipe.GetComposeFiles(app.Env)
		if err != nil {
			log.Fatal(err)
		}

		stackName := app.StackName()
		deployOpts := stack.Deploy{
			Composefiles: composeFiles,
			Namespace:    stackName,
			Prune:        false,
			ResolveImage: stack.ResolveImageAlways,
			Detach:       false,
		}
		compose, err := appPkg.GetAppComposeConfig(app.Name, deployOpts, app.Env)
		if err != nil {
			log.Fatal(err)
		}

		toDeployChaosVersionLabel := toDeployChaosVersion
		if app.Recipe.Dirty {
			toDeployChaosVersionLabel = formatter.AddDirtyMarker(toDeployChaosVersionLabel)
		}

		appPkg.ExposeAllEnv(stackName, compose, app.Env)
		appPkg.SetRecipeLabel(compose, stackName, app.Recipe.Name)
		appPkg.SetChaosLabel(compose, stackName, internal.Chaos)
		if internal.Chaos {
			appPkg.SetChaosVersionLabel(compose, stackName, toDeployChaosVersionLabel)
		}
		appPkg.SetUpdateLabel(compose, stackName, app.Env)

		envVars, err := appPkg.CheckEnv(app)
		if err != nil {
			log.Fatal(err)
		}

		for _, envVar := range envVars {
			if !envVar.Present {
				deployWarnMessages = append(deployWarnMessages,
					fmt.Sprintf("%s missing from %s.env", envVar.Name, app.Domain),
				)
			}
		}

		if !internal.NoDomainChecks {
			if domainName, ok := app.Env["DOMAIN"]; ok {
				if _, err = dns.EnsureDomainsResolveSameIPv4(domainName, app.Server); err != nil {
					log.Fatal(err)
				}
			} else {
				log.Debug("skipping domain checks, no DOMAIN=... configured")
			}
		} else {
			log.Debug("skipping domain checks")
		}

		deployedVersion := config.NO_VERSION_DEFAULT
		if deployMeta.IsDeployed {
			deployedVersion = deployMeta.Version
		}

		toWriteVersion := toDeployVersion
		if internal.Chaos || isChaosCommit {
			toWriteVersion = toDeployChaosVersion
		}

		if err := internal.DeployOverview(
			app,
			deployWarnMessages,
			deployedVersion,
			deployMeta.ChaosVersion,
			toDeployVersion,
			toDeployChaosVersion,
			toWriteVersion,
		); err != nil {
			log.Fatal(err)
		}

		stack.WaitTimeout, err = appPkg.GetTimeoutFromLabel(compose, stackName)
		if err != nil {
			log.Fatal(err)
		}

		log.Debugf("set waiting timeout to %d second(s)", stack.WaitTimeout)

		if err := stack.RunDeploy(cl, deployOpts, compose, app.Name, internal.DontWaitConverge); err != nil {
			log.Fatal(err)
		}

		postDeployCmds, ok := app.Env["POST_DEPLOY_CMDS"]
		if ok && !internal.DontWaitConverge {
			log.Debugf("run the following post-deploy commands: %s", postDeployCmds)
			if err := internal.PostCmds(cl, app, postDeployCmds); err != nil {
				log.Fatalf("attempting to run post deploy commands, saw: %s", err)
			}
		}

		if err := app.WriteRecipeVersion(toWriteVersion, false); err != nil {
			log.Fatalf("writing recipe version failed: %s", err)
		}
	},
}

func getChaosVersion(app app.App, toDeployVersion, toDeployChaosVersion *string) error {
	var err error
	*toDeployChaosVersion, err = app.Recipe.ChaosVersion()
	if err != nil {
		return err
	}

	*toDeployVersion, err = app.Recipe.GetVersionLabelLocal()
	if err != nil {
		return err
	}

	return nil
}

func getLatestVersionOrCommit(app app.App, toDeployVersion *string) error {
	versions, err := app.Recipe.Tags()
	if err != nil {
		return err
	}

	if len(versions) > 0 && !internal.Chaos {
		*toDeployVersion = versions[len(versions)-1]

		log.Debugf("choosing %s as version to deploy", *toDeployVersion)

		if _, err := app.Recipe.EnsureVersion(*toDeployVersion); err != nil {
			return err
		}

		return nil
	}

	head, err := app.Recipe.Head()
	if err != nil {
		return err
	}

	*toDeployVersion = formatter.SmallSHA(head.String())

	return nil
}

// validateArgsAndFlags ensures compatible args/flags.
func validateArgsAndFlags(args []string) error {
	if len(args) == 2 && args[1] != "" && internal.Chaos {
		return fmt.Errorf("cannot use [version] and --chaos together")
	}

	return nil
}

func validateSecrets(cl *dockerClient.Client, app app.App) error {
	secStats, err := secret.PollSecretsStatus(cl, app)
	if err != nil {
		return err
	}

	for _, secStat := range secStats {
		if !secStat.CreatedOnRemote {
			return fmt.Errorf("secret not generated: %s", secStat.LocalName)
		}
	}

	return nil
}

func init() {
	AppDeployCommand.Flags().BoolVarP(
		&internal.Chaos,
		"chaos",
		"C",
		false,
		"ignore uncommitted recipes changes",
	)

	AppDeployCommand.Flags().BoolVarP(
		&internal.Force,
		"force",
		"f",
		false,
		"perform action without further prompt",
	)

	AppDeployCommand.Flags().BoolVarP(
		&internal.NoDomainChecks,
		"no-domain-checks",
		"D",
		false,
		"disable public DNS checks",
	)

	AppDeployCommand.Flags().BoolVarP(
		&internal.DontWaitConverge,
		"no-converge-checks",
		"c",
		false,
		"disable converge logic checks",
	)
}
