package importProfile

import (
	"github.com/spf13/cobra"
	"github.com/stackitcloud/stackit-cli/internal/pkg/args"
	"github.com/stackitcloud/stackit-cli/internal/pkg/config"
	"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
	"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
	"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
	"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
	"github.com/stackitcloud/stackit-cli/internal/pkg/print"
	"github.com/stackitcloud/stackit-cli/internal/pkg/types"
)

const (
	nameFlag   = "name"
	configFlag = "config"
	noSetFlag  = "no-set"
)

type inputModel struct {
	*globalflags.GlobalFlagModel
	ProfileName string
	Config      string
	NoSet       bool
}

func NewCmd(params *types.CmdParams) *cobra.Command {
	cmd := &cobra.Command{
		Use:   "import",
		Short: "Imports a CLI configuration profile",
		Long:  "Imports a CLI configuration profile.",
		Example: examples.Build(
			examples.NewExample(
				`Import a config with name "PROFILE_NAME" from file "./config.json"`,
				"$ stackit config profile import --name PROFILE_NAME --config `@./config.json`",
			),
			examples.NewExample(
				`Import a config with name "PROFILE_NAME" from file "./config.json" and do not set as active`,
				"$ stackit config profile import --name PROFILE_NAME --config `@./config.json` --no-set",
			),
		),
		Args: args.NoArgs,
		RunE: func(cmd *cobra.Command, args []string) error {
			model, err := parseInput(params.Printer, cmd, args)
			if err != nil {
				return err
			}

			err = config.ImportProfile(params.Printer, model.ProfileName, model.Config, !model.NoSet)
			if err != nil {
				return err
			}

			params.Printer.Info("Successfully imported profile %q\n", model.ProfileName)

			return nil
		},
	}
	configureFlags(cmd)
	return cmd
}

func configureFlags(cmd *cobra.Command) {
	cmd.Flags().String(nameFlag, "", "Profile name")
	cmd.Flags().VarP(flags.ReadFromFileFlag(), configFlag, "c", "File where configuration will be imported from")
	cmd.Flags().Bool(noSetFlag, false, "Set the imported profile not as active")

	cobra.CheckErr(cmd.MarkFlagRequired(nameFlag))
	cobra.CheckErr(cmd.MarkFlagRequired(configFlag))
}

func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
	globalFlags := globalflags.Parse(p, cmd)

	model := &inputModel{
		GlobalFlagModel: globalFlags,
		ProfileName:     flags.FlagToStringValue(p, cmd, nameFlag),
		Config:          flags.FlagToStringValue(p, cmd, configFlag),
		NoSet:           flags.FlagToBoolValue(p, cmd, noSetFlag),
	}

	if model.Config == "" {
		return nil, &errors.FlagValidationError{
			Flag:    configFlag,
			Details: "must not be empty",
		}
	}

	if model.ProfileName == "" {
		return nil, &errors.FlagValidationError{
			Flag:    nameFlag,
			Details: "must not be empty",
		}
	}

	p.DebugInputModel(model)
	return model, nil
}
