package admin

import (
	"fmt"
	"os"

	cli "github.com/danieljustus/symaira-vault/internal/cli"

	"github.com/spf13/cobra"
	"golang.org/x/term"

	"github.com/danieljustus/symaira-vault/internal/ui/wizard"
)

var setupKeepOnError bool
var setupNoResume bool

var setupCmd = &cobra.Command{
	Use:   "setup",
	Short: "Interactive setup wizard for Symaira Vault",
	Long: `Launch the interactive setup wizard to initialize or re-configure your vault.

The wizard guides you through:
  • Vault directory and passphrase
  • Authentication method (Touch ID or passphrase)
  • Sync strategy (local or git remote)
  • Multi-device setup hints
  • Additional recipients for shared access
  • AI agent (MCP) configuration
  • Backup recommendations
  • Profile name

For non-interactive environments (CI, scripts), use 'symvault init' instead.`,
	Example: `  # Run the wizard (resumes from saved state if available)
  symvault setup

  # Restart from scratch
  symvault setup --no-resume

  # Keep partial vault artifacts on error for debugging
  symvault setup --keep-on-error`,
	Annotations: map[string]string{
		cli.RequiresVaultAnnotation: "false",
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		// WIZ-15: Non-TTY guard.
		if !term.IsTerminal(int(os.Stdin.Fd())) {
			return fmt.Errorf("setup needs a TTY; use `symvault init` for non-interactive vault initialization")
		}

		vaultDir := cli.GetVaultDir()
		return wizard.Run(vaultDir, setupKeepOnError, setupNoResume)
	},
}

func init() {
	setupCmd.Flags().BoolVar(&setupKeepOnError, "keep-on-error", false, "do not rollback vault init artifacts when subsequent steps fail")
	setupCmd.Flags().BoolVar(&setupNoResume, "no-resume", false, "disable setup resume after abort")
	cli.RootCmd.AddCommand(setupCmd)
}
