package app

import (
	"context"
	"fmt"

	"coopcloud.tech/abra/cli/internal"
	"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"
	"github.com/urfave/cli"
)

var appDeployCommand = cli.Command{
	Name:      "deploy",
	Aliases:   []string{"d"},
	Usage:     "Deploy an app",
	ArgsUsage: "<domain> [<version>]",
	Flags: []cli.Flag{
		internal.DebugFlag,
		internal.NoInputFlag,
		internal.ForceFlag,
		internal.ChaosFlag,
		internal.NoDomainChecksFlag,
		internal.DontWaitConvergeFlag,
		internal.OfflineFlag,
	},
	Before: internal.SubCommandBefore,
	Description: `Deploy an app.

This command supports chaos operations. Use "--chaos" to deploy your recipe
checkout as-is. Recipe commit hashes are also supported values for
"[<version>]". Please note, "upgrade"/"rollback" do not support chaos
operations.

EXAMPLE:

    abra app deploy foo.example.com
    abra app deploy foo.example.com 1.2.3+3.2.1
    abra app deploy foo.example.com 1e83340e`,
	BashComplete: autocomplete.AppNameComplete,
	Action: func(c *cli.Context) error {
		var warnMessages []string

		app := internal.ValidateApp(c)
		stackName := app.StackName()

		specificVersion := c.Args().Get(1)
		if specificVersion != "" && internal.Chaos {
			log.Fatal("cannot use <version> and --chaos together")
		}

		if specificVersion != "" {
			log.Debugf("overriding env file version (%s) with %s", app.Recipe.Version, specificVersion)
			app.Recipe.Version = specificVersion
		}

		if specificVersion == "" && app.Recipe.Version != "" && !internal.Chaos {
			log.Debugf("retrieved %s as version from env file", app.Recipe.Version)
			specificVersion = app.Recipe.Version
		}

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

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

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

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

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

		// NOTE(d1): handles "<version> as git hash" use case
		var isChaosCommit bool

		// NOTE(d1): check out specific version before dealing with secrets. This
		// is because we need to deal with GetComposeFiles under the hood and these
		// files change from version to version which therefore affects which
		// secrets might be generated
		version := deployMeta.Version
		if specificVersion != "" {
			version = specificVersion
			log.Debugf("choosing %s as version to deploy", version)

			var err error
			isChaosCommit, err = app.Recipe.EnsureVersion(version)
			if err != nil {
				log.Fatal(err)
			}

			if isChaosCommit {
				log.Debugf("assuming '%s' is a chaos commit", version)
				internal.Chaos = true
			}
		}

		secStats, err := secret.PollSecretsStatus(cl, app)
		if err != nil {
			log.Fatal(err)
		}

		for _, secStat := range secStats {
			if !secStat.CreatedOnRemote {
				log.Fatalf("unable to deploy, secrets not generated (%s)?", secStat.LocalName)
			}
		}

		if deployMeta.IsDeployed {
			if internal.Force || internal.Chaos {
				warnMessages = append(warnMessages, fmt.Sprintf("%s is already deployed", app.Name))
			} else {
				log.Fatalf("%s is already deployed", app.Name)
			}
		}

		if !internal.Chaos && specificVersion == "" {
			versions, err := app.Recipe.Tags()
			if err != nil {
				log.Fatal(err)
			}

			if len(versions) > 0 && !internal.Chaos {
				version = versions[len(versions)-1]
				log.Debugf("choosing %s as version to deploy", version)
				if _, err := app.Recipe.EnsureVersion(version); err != nil {
					log.Fatal(err)
				}
			} else {
				head, err := app.Recipe.Head()
				if err != nil {
					log.Fatal(err)
				}
				version = formatter.SmallSHA(head.String())
				warnMessages = append(warnMessages, fmt.Sprintf("no versions detected, using latest commit"))
			}
		}

		chaosVersion := config.CHAOS_DEFAULT
		if internal.Chaos {
			warnMessages = append(warnMessages, "chaos mode engaged")

			if isChaosCommit {
				chaosVersion = specificVersion
				versionLabelLocal, err := app.Recipe.GetVersionLabelLocal()
				if err != nil {
					log.Fatal(err)
				}
				version = versionLabelLocal
			} else {
				var err error
				chaosVersion, err = app.Recipe.ChaosVersion()
				if 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)
		}

		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)
		}

		appPkg.ExposeAllEnv(stackName, compose, app.Env)
		appPkg.SetRecipeLabel(compose, stackName, app.Recipe.Name)
		appPkg.SetChaosLabel(compose, stackName, internal.Chaos)
		appPkg.SetChaosVersionLabel(compose, stackName, chaosVersion)
		appPkg.SetUpdateLabel(compose, stackName, app.Env)

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

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

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

		if err := internal.DeployOverview(app, warnMessages, version, chaosVersion); 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 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)
			}
		}

		app.Recipe.Version = version
		if chaosVersion != config.CHAOS_DEFAULT {
			app.Recipe.Version = chaosVersion
		}
		log.Debugf("choosing %s as version to save to env file", app.Recipe.Version)
		if err := app.WriteRecipeVersion(app.Recipe.Version, false); err != nil {
			log.Fatalf("writing new recipe version in env file: %s", err)
		}

		return nil
	},
}
