package cmd

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/spf13/cobra"
	appconfig "github.com/yourorg/devour/internal/config"
)

var initCmd = &cobra.Command{
	Use:   "init [path]",
	Short: "Initialize Devour for a project",
	Long: `Initialize Devour configuration for a project.

Creates a devour.yaml configuration file and the necessary directory
structure for storing documents and indexes.

Examples:
  devour init                    # Initialize in current directory
  devour init ./my-project       # Initialize in specific directory
  devour init --remote           # Initialize with remote mode config`,
	RunE: runInit,
}

var (
	initRemote bool
	initForce  bool
)

func init() {
	initCmd.Flags().BoolVar(&initRemote, "remote", false, "configure for remote mode")
	initCmd.Flags().BoolVarP(&initForce, "force", "f", false, "overwrite existing config")
}

func runInit(cmd *cobra.Command, args []string) error {
	// Determine target directory
	targetDir := "."
	if len(args) > 0 {
		targetDir = args[0]
	}

	// Create directory if it doesn't exist
	if err := os.MkdirAll(targetDir, 0755); err != nil {
		return fmt.Errorf("failed to create directory: %w", err)
	}

	configPath := filepath.Join(targetDir, "devour.yaml")

	// Check if config already exists
	if _, err := os.Stat(configPath); err == nil && !initForce {
		return fmt.Errorf("config file already exists at %s (use --force to overwrite)", configPath)
	}

	// Create default config
	config, err := appconfig.RenderInitYAML(initRemote)
	if err != nil {
		return fmt.Errorf("failed to render default config: %w", err)
	}
	if err := os.WriteFile(configPath, []byte(config), 0644); err != nil {
		return fmt.Errorf("failed to write config: %w", err)
	}

	// Create data directories
	dataDir := filepath.Join(targetDir, "devour_data")
	dirs := []string{
		filepath.Join(dataDir, "docs"),
		filepath.Join(dataDir, "index"),
		filepath.Join(dataDir, "metadata"),
		filepath.Join(dataDir, "cache"),
	}

	for _, dir := range dirs {
		if err := os.MkdirAll(dir, 0755); err != nil {
			return fmt.Errorf("failed to create directory %s: %w", dir, err)
		}
	}

	fmt.Printf("✓ Created config at %s\n", configPath)
	fmt.Printf("✓ Created data directories at %s\n", dataDir)
	fmt.Println("\nNext steps:")
	fmt.Println("  1. Set OPENAI_API_KEY environment variable")
	fmt.Println("  2. Add sources to devour.yaml")
	fmt.Println("  3. Run 'devour scrape <source>' to index documentation")

	return nil
}
