package app

import (
	"fmt"
	"strings"

	"coopcloud.tech/abra/cli/internal"
	"coopcloud.tech/abra/pkg/autocomplete"
	"coopcloud.tech/abra/pkg/client"
	"coopcloud.tech/abra/pkg/log"
	"github.com/spf13/cobra"
)

var AppRestoreCommand = &cobra.Command{
	Use:     "restore <app> [flags]",
	Aliases: []string{"rs"},
	Short:   "Restore a snapshot",
	Long: `Snapshots are restored while apps are deployed.

Some restore scenarios may require service / app restarts.`,
	Args: cobra.ExactArgs(1),
	ValidArgsFunction: func(
		cmd *cobra.Command,
		args []string,
		toComplete string) ([]string, cobra.ShellCompDirective) {
		return autocomplete.AppNameComplete()
	},
	Run: func(cmd *cobra.Command, args []string) {
		app := internal.ValidateApp(args)

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

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

		targetContainer, err := internal.RetrieveBackupBotContainer(cl)
		if err != nil {
			log.Fatal(err)
		}

		execEnv := []string{
			fmt.Sprintf("SERVICE=%s", app.Domain),
			"MACHINE_LOGS=true",
		}

		if snapshot != "" {
			log.Debugf("including SNAPSHOT=%s in backupbot exec invocation", snapshot)
			execEnv = append(execEnv, fmt.Sprintf("SNAPSHOT=%s", snapshot))
		}

		if targetPath != "" {
			log.Debugf("including TARGET=%s in backupbot exec invocation", targetPath)
			execEnv = append(execEnv, fmt.Sprintf("TARGET=%s", targetPath))
		}

		if internal.NoInput {
			log.Debugf("including NONINTERACTIVE=%v in backupbot exec invocation", internal.NoInput)
			execEnv = append(execEnv, fmt.Sprintf("NONINTERACTIVE=%v", internal.NoInput))
		}

		if len(volumes) > 0 {
			allVolumes := strings.Join(volumes, ",")
			log.Debugf("including VOLUMES=%s in backupbot exec invocation", allVolumes)
			execEnv = append(execEnv, fmt.Sprintf("VOLUMES=%s", allVolumes))
		}

		if len(services) > 0 {
			allServices := strings.Join(services, ",")
			log.Debugf("including CONTAINER=%s in backupbot exec invocation", allServices)
			execEnv = append(execEnv, fmt.Sprintf("CONTAINER=%s", allServices))
		}

		if hooks {
			log.Debugf("including NO_COMMANDS=%v in backupbot exec invocation", false)
			execEnv = append(execEnv, fmt.Sprintf("NO_COMMANDS=%v", false))
		}

		if _, err := internal.RunBackupCmdRemote(cl, "restore", targetContainer.ID, execEnv); err != nil {
			log.Fatal(err)
		}
	},
}

var (
	targetPath string
	hooks      bool
	services   []string
	volumes    []string
)

func init() {
	AppRestoreCommand.Flags().StringVarP(
		&targetPath,
		"target",
		"t",
		"/",
		"target path",
	)

	AppRestoreCommand.Flags().StringArrayVarP(
		&services,
		"services",
		"s",
		[]string{},
		"restore specific services",
	)

	AppRestoreCommand.Flags().StringArrayVarP(
		&volumes,
		"volumes",
		"v",
		[]string{},
		"restore specific volumes",
	)

	AppRestoreCommand.Flags().BoolVarP(
		&hooks,
		"hooks",
		"H",
		false,
		"enable pre/post-hook command execution",
	)

	AppRestoreCommand.Flags().BoolVarP(
		&internal.Chaos,
		"chaos",
		"C",
		false,
		"ignore uncommitted recipes changes",
	)
}
