package commands

import (
	"fmt"

	"github.com/muyue/muyue/internal/config"
	"github.com/muyue/muyue/internal/profiler"
	"github.com/spf13/cobra"
)

var setupCmd = &cobra.Command{
	Use:   "setup",
	Short: "Run first-run wizard (profiler)",
	RunE:  runSetup,
}

func init() {
	rootCmd.AddCommand(setupCmd)
}

func runSetup(cmd *cobra.Command, args []string) error {
	cfg, err := profiler.RunFirstTimeSetup()
	if err != nil {
		return err
	}
	for i := range cfg.AI.Providers {
		if cfg.AI.Providers[i].Active && cfg.AI.Providers[i].APIKey == "" {
			key, err := profiler.AskAPIKey(cfg.AI.Providers[i].Name)
			if err == nil && key != "" {
				cfg.AI.Providers[i].APIKey = key
			}
		}
	}
	if err := config.Save(cfg); err != nil {
		return err
	}
	fmt.Println("Setup complete!")
	return nil
}