"""Engine configuration settings."""

from dataclasses import dataclass, field
from typing import Literal


@dataclass
class EngineConfig:
    """Configuration for the XYGLUE Engine."""

    # Mode
    mode: Literal["semi", "auto"] = "semi"
    divergence: Literal["low", "medium", "high"] = "medium"
    learning: Literal["supervised", "auto"] = "supervised"

    # Agent names (user-customizable)
    agent_names: dict[str, str] = field(default_factory=lambda: {
        "scout": "Scout",
        "physicist": "Physicist",
        "architect": "Architect",
    })

    # Execution settings
    default_timeout_ms: int = 300_000
    max_retries: int = 3
    retry_delay_ms: int = 1000

    # Healing settings
    max_heals_per_step: int = 3
    max_heals_per_run: int = 10
    auto_heal_confidence_threshold: float = 0.9

    # Template library settings
    template_quality_threshold: float = 0.95
    template_proven_count: int = 2
    template_failure_resets: bool = True

    # Pattern library settings
    max_patterns: int = 500
    min_confidence_to_keep: float = 0.5
    decay_rate_per_day: float = 0.001

    # AI settings
    ai_model: str = "claude-sonnet-4-20250514"
    ai_max_tokens: int = 4096

    # Physics guardrails (always on)
    max_budget_per_run_usd: float = 1.0
    max_run_duration_ms: int = 600_000
    allowed_tools: list[str] = field(default_factory=lambda: [
        "browser", "http", "ai", "email", "files", "database", "code"
    ])

    redact_sensitive_data: bool = True

    def validate(self) -> list[str]:
        issues = []
        if not (0 <= self.template_quality_threshold <= 1):
            issues.append("template_quality_threshold must be 0-1")
        if self.template_proven_count < 1:
            issues.append("template_proven_count must be >= 1")
        if self.max_retries < 0:
            issues.append("max_retries must be >= 0")
        return issues
