// Package config loads and validates YAML configuration for odidere.
package config

import (
	"fmt"
	"net/url"
	"os"
	"regexp"
	"time"

	"gopkg.in/yaml.v3"
)

// TTS holds the configuration for TTS.
type TTS struct {
	// URL is the base URL of kokoro-fastapi.
	// For example, "http://localhost:8880".
	URL string `yaml:"url"`
	// Voice is the default voice ID to use (e.g., "af_heart").
	Voice string `yaml:"voice"`
	// Timeout is the maximum duration for a synthesis request.
	// Defaults to 60s if empty.
	Timeout string `yaml:"timeout"`
}

func (cfg TTS) Validate() error {
	if cfg.URL == "" {
		return fmt.Errorf("missing URL")
	}
	if _, err := url.Parse(cfg.URL); err != nil {
		return fmt.Errorf("invalid URL %q: %w", cfg.URL, err)
	}
	if cfg.Voice == "" {
		return fmt.Errorf("missing voice")
	}
	if cfg.Timeout != "" {
		if _, err := time.ParseDuration(cfg.Timeout); err != nil {
			return fmt.Errorf("invalid timeout: %w", err)
		}
	}
	return nil
}

// STT holds the configuration for the frontend-facing speech-to-text server.
type STT struct {
	// URL is the base URL of the whisper-server.
	// For example, "http://localhost:8178".
	URL string `yaml:"url"`
}

func (cfg STT) Validate() error {
	if cfg.URL == "" {
		return fmt.Errorf("missing URL")
	}
	if _, err := url.Parse(cfg.URL); err != nil {
		return fmt.Errorf("invalid URL %q: %w", cfg.URL, err)
	}
	return nil
}

// ProviderConfig holds the configuration for a single LLM provider.
type ProviderConfig struct {
	// Name uniquely identifies this provider (e.g., "llama.cpp", "openrouter").
	Name string `yaml:"name"`
	// URL is the base URL of the OpenAI-compatible API endpoint.
	URL string `yaml:"url"`
	// Key is the API key for authentication.
	Key string `yaml:"key"`
	// Timeout is the maximum duration for a query (e.g., "5m").
	// Defaults to 5 minutes if empty.
	Timeout string `yaml:"timeout"`
	// Models is an optional whitelist of model IDs for this provider.
	Models []string `yaml:"models,omitempty"`
}

func (cfg ProviderConfig) Validate() error {
	if cfg.Name == "" {
		return fmt.Errorf("missing name")
	}
	if cfg.URL == "" {
		return fmt.Errorf("missing URL")
	}
	if _, err := url.Parse(cfg.URL); err != nil {
		return fmt.Errorf("invalid URL %q: %w", cfg.URL, err)
	}
	if cfg.Timeout != "" {
		if _, err := time.ParseDuration(cfg.Timeout); err != nil {
			return fmt.Errorf("invalid timeout: %w", err)
		}
	}
	return nil
}

// ModelConfig specifies the provider and model to use.
type ModelConfig struct {
	// Provider is the provider name (e.g., "llama.cpp").
	Provider string `yaml:"provider,omitempty"`
	// Model is the model ID within that provider (e.g., "qwen3.6-27b").
	Model string `yaml:"model,omitempty"`
}

// JobConfig holds the user-facing configuration for a job.
type JobConfig struct {
	// MaxIterations caps the number of agent loop iterations.
	MaxIterations int `yaml:"max_iterations,omitempty"`
	// Name uniquely identifies the job within the config.
	Name string `yaml:"name"`
	// Model is the optional model for this job.
	Model ModelConfig `yaml:"model,omitempty"`
	// Schedule is a cron expression (e.g., "0 9 * * *").
	Schedule string `yaml:"schedule"`
	// SystemMessage overrides the global system message for this job.
	SystemMessage string `yaml:"system_message,omitempty"`
	// Task is the user-level instruction to give the LLM.
	Task string `yaml:"task"`
	// Timeout limits the job execution time (e.g., "10m").
	Timeout string `yaml:"timeout,omitempty"`
}

func (cfg JobConfig) Validate() error {
	if cfg.Name == "" {
		return fmt.Errorf("missing name")
	}
	if cfg.Task == "" {
		return fmt.Errorf("missing task")
	}
	if cfg.Schedule == "" {
		return fmt.Errorf("missing schedule")
	}
	if cfg.MaxIterations < 0 {
		return fmt.Errorf("max_iterations must be non-negative")
	}
	return nil
}

// ToolConfig represents an external tool definition.
type ToolConfig struct {
	// Name uniquely identifies the tool within a registry.
	Name string `yaml:"name"`
	// Description explains the tool's purpose for the LLM.
	Description string `yaml:"description"`
	// Command is the executable path or name.
	Command string `yaml:"command"`
	// Arguments are Go templates expanded with LLM-provided parameters.
	Arguments []string `yaml:"arguments"`
	// Parameters is a JSON Schema describing expected input from the LLM.
	Parameters map[string]any `yaml:"parameters"`
	// Timeout limits execution time (e.g., "30s", "5m").
	Timeout string `yaml:"timeout"`
}

func (cfg ToolConfig) Validate() error {
	if cfg.Name == "" {
		return fmt.Errorf("missing name")
	}
	if cfg.Description == "" {
		return fmt.Errorf("missing description")
	}
	if cfg.Command == "" {
		return fmt.Errorf("missing command")
	}
	if cfg.Timeout != "" {
		if _, err := time.ParseDuration(cfg.Timeout); err != nil {
			return fmt.Errorf("invalid timeout: %v", err)
		}
	}
	return nil
}

// Config holds all application configuration sections.
type Config struct {
	// Address is the host:port to listen on.
	Address string `yaml:"address"`
	// ShutdownTimeout is the maximum time to wait for graceful shutdown.
	ShutdownTimeout string `yaml:"shutdown_timeout"`
	// shutdownTimeout is the parsed duration, set during Load.
	shutdownTimeout time.Duration `yaml:"-"`
	// SystemMessage is the default system prompt prepended to all conversations.
	SystemMessage string `yaml:"system_message"`
	// DefaultModel specifies the default provider and model.
	DefaultModel ModelConfig `yaml:"default_model"`
	// Providers defines the available LLM providers.
	Providers []ProviderConfig `yaml:"providers"`
	// STT configures the speech-to-text server URL passed to the frontend.
	STT STT `yaml:"stt"`
	// Jobs defines scheduled autonomous tasks.
	Jobs []JobConfig `yaml:"jobs"`
	// Tools defines external commands available to the LLM.
	Tools []ToolConfig `yaml:"tools"`
	// TTS configures the text-to-speech client.
	TTS TTS `yaml:"tts"`
}

// ApplyDefaults sets default values for optional configuration fields.
func (cfg *Config) ApplyDefaults() {
	if cfg.ShutdownTimeout == "" {
		cfg.ShutdownTimeout = "30s"
	}
	if cfg.Address == "" {
		cfg.Address = ":8080"
	}
}

// GetShutdownTimeout returns the parsed shutdown timeout duration.
func (cfg *Config) GetShutdownTimeout() time.Duration {
	return cfg.shutdownTimeout
}

// Validate checks that all required fields are present and valid.
func (cfg *Config) Validate() error {
	if cfg.Address == "" {
		return fmt.Errorf("address required")
	}
	if cfg.DefaultModel.Provider == "" {
		return fmt.Errorf("default_model.provider required")
	}
	if cfg.DefaultModel.Model == "" {
		return fmt.Errorf("default_model.model required")
	}
	if len(cfg.Providers) == 0 {
		return fmt.Errorf("at least one provider required")
	}
	for i, p := range cfg.Providers {
		if err := p.Validate(); err != nil {
			return fmt.Errorf("providers[%d] invalid: %w", i, err)
		}
	}
	if err := cfg.STT.Validate(); err != nil {
		return fmt.Errorf("invalid stt config: %w", err)
	}
	if err := cfg.TTS.Validate(); err != nil {
		return fmt.Errorf("invalid tts config: %w", err)
	}
	if _, err := time.ParseDuration(cfg.ShutdownTimeout); err != nil {
		return fmt.Errorf("invalid shutdown_timeout %q: %w", cfg.ShutdownTimeout, err)
	}
	for i, jc := range cfg.Jobs {
		if err := jc.Validate(); err != nil {
			return fmt.Errorf("jobs[%d] invalid: %w", i, err)
		}
	}
	for i, tc := range cfg.Tools {
		if err := tc.Validate(); err != nil {
			return fmt.Errorf("tools[%d] invalid: %w", i, err)
		}
	}
	return nil
}

// Load reads a YAML configuration file, expands environment variables,
// applies defaults, and validates the result.
func Load(path string) (*Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, fmt.Errorf("read file: %w", err)
	}

	// Expand environment variables.
	re := regexp.MustCompile(`\$\{([^}]+)\}`)
	expanded := re.ReplaceAllStringFunc(string(data), func(match string) string {
		v := match[2 : len(match)-1]
		return os.Getenv(v)
	})

	var cfg Config
	if err := yaml.Unmarshal([]byte(expanded), &cfg); err != nil {
		return nil, fmt.Errorf("parse config: %w", err)
	}

	cfg.ApplyDefaults()
	if err := cfg.Validate(); err != nil {
		return nil, fmt.Errorf("invalid config: %v", err)
	}

	d, err := time.ParseDuration(cfg.ShutdownTimeout)
	if err != nil {
		return nil, fmt.Errorf("parse shutdown timeout: %v", err)
	}
	cfg.shutdownTimeout = d

	return &cfg, nil
}
