package app

import (
	"context"

	"coopcloud.tech/abra/cli/internal"
	"coopcloud.tech/abra/pkg/autocomplete"
	"coopcloud.tech/abra/pkg/client"
	"coopcloud.tech/abra/pkg/formatter"
	"coopcloud.tech/abra/pkg/log"
	"coopcloud.tech/abra/pkg/upstream/stack"
	"github.com/AlecAivazis/survey/v2"
	"github.com/spf13/cobra"
)

var AppVolumeListCommand = &cobra.Command{
	Use:     "list <app> [flags]",
	Aliases: []string{"ls"},
	Short:   "List volumes associated with an app",
	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)

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

		filters, err := app.Filters(false, true)
		if err != nil {
			log.Fatal(err)
		}

		volumes, err := client.GetVolumes(cl, context.Background(), app.Server, filters)
		if err != nil {
			log.Fatal(err)
		}

		headers := []string{"NAME", "ON SERVER"}

		table, err := formatter.CreateTable()
		if err != nil {
			log.Fatal(err)
		}

		table.Headers(headers...)

		var rows [][]string
		for _, volume := range volumes {
			row := []string{volume.Name, volume.Mountpoint}
			rows = append(rows, row)
		}

		table.Rows(rows...)

		if len(rows) > 0 {
			if err := formatter.PrintTable(table); err != nil {
				log.Fatal(err)
			}
			return
		}

		log.Warnf("no volumes created for %s", app.Name)
	},
}

var AppVolumeRemoveCommand = &cobra.Command{
	Use:   "remove <app> [flags]",
	Short: "Remove volume(s) associated with an app",
	Long: `Remove volumes associated with an app.

The app in question must be undeployed before you try to remove volumes. See
"abra app undeploy <app>" for more.

The command is interactive and will show a multiple select input which allows
you to make a seclection. Use the "?" key to see more help on navigating this
interface.

Passing "--force/-f" will select all volumes for removal. Be careful.`,
	Aliases: []string{"rm"},
	Args:    cobra.MinimumNArgs(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)

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

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

		if deployMeta.IsDeployed {
			log.Fatalf("%s is still deployed. Run \"abra app undeploy %s\"", app.Name, app.Name)
		}

		filters, err := app.Filters(false, true)
		if err != nil {
			log.Fatal(err)
		}

		volumeList, err := client.GetVolumes(cl, context.Background(), app.Server, filters)
		if err != nil {
			log.Fatal(err)
		}
		volumeNames := client.GetVolumeNames(volumeList)

		var volumesToRemove []string
		if !internal.Force && !internal.NoInput {
			volumesPrompt := &survey.MultiSelect{
				Message: "which volumes do you want to remove?",
				Help:    "'x' indicates selected, enter / return to confirm, ctrl-c to exit, vim mode is enabled",
				VimMode: true,
				Options: volumeNames,
				Default: volumeNames,
			}
			if err := survey.AskOne(volumesPrompt, &volumesToRemove); err != nil {
				log.Fatal(err)
			}
		}

		if internal.Force || internal.NoInput {
			volumesToRemove = volumeNames
		}

		if len(volumesToRemove) > 0 {
			err := client.RemoveVolumes(cl, context.Background(), volumesToRemove, internal.Force, 5)
			if err != nil {
				log.Fatalf("removing volumes failed: %s", err)
			}

			log.Infof("%d volumes removed successfully", len(volumesToRemove))
		} else {
			log.Info("no volumes removed")
		}
	},
}

var AppVolumeCommand = &cobra.Command{
	Use:     "volume [cmd] [args] [flags]",
	Aliases: []string{"vl"},
	Short:   "Manage app volumes",
}

func init() {
	AppVolumeRemoveCommand.Flags().BoolVarP(
		&internal.Force,
		"force",
		"f",
		false,
		"perform action without further prompt",
	)
}
