"""Application settings for PentestAgent."""

import os
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional

from .constants import (
    AGENT_MAX_ITERATIONS,
    DEFAULT_MAX_TOKENS,
    DEFAULT_MODEL,
    DEFAULT_TEMPERATURE,
)


@dataclass
class Settings:
    """Application settings."""

    # LLM Settings
    model: str = field(default_factory=lambda: DEFAULT_MODEL)
    temperature: float = DEFAULT_TEMPERATURE
    max_tokens: int = DEFAULT_MAX_TOKENS
    max_context_tokens: int = 128000

    # API Keys (loaded from environment)
    openai_api_key: Optional[str] = field(
        default_factory=lambda: os.getenv("OPENAI_API_KEY")
    )
    anthropic_api_key: Optional[str] = field(
        default_factory=lambda: os.getenv("ANTHROPIC_API_KEY")
    )

    # Paths
    knowledge_path: Path = field(default_factory=lambda: Path("knowledge"))
    mcp_config_path: Path = field(default_factory=lambda: Path("mcp.json"))

    # Docker Settings
    container_name: str = "pentestagent-sandbox"
    docker_image: str = "ghcr.io/gh05tcrew/pentestagent:kali"

    # Agent Settings
    max_iterations: int = AGENT_MAX_ITERATIONS

    # VPN Settings
    vpn_config_path: Optional[Path] = None

    # Interface Settings
    default_interface: str = "tui"  # "tui" or "cli"

    # Prompt Modules
    prompt_modules: List[str] = field(default_factory=list)

    # Target Settings
    target: Optional[str] = None
    scope: List[str] = field(default_factory=list)

    def __post_init__(self):
        """Convert string paths to Path objects if needed."""
        if isinstance(self.knowledge_path, str):
            self.knowledge_path = Path(self.knowledge_path)
        if isinstance(self.mcp_config_path, str):
            self.mcp_config_path = Path(self.mcp_config_path)
        if isinstance(self.vpn_config_path, str):
            self.vpn_config_path = Path(self.vpn_config_path)


# Global settings instance
_settings: Optional[Settings] = None


def get_settings() -> Settings:
    """Get the global settings instance."""
    global _settings
    if _settings is None:
        _settings = Settings()
    return _settings


def update_settings(**kwargs) -> Settings:
    """Update global settings with new values."""
    global _settings
    _settings = Settings(**kwargs)
    return _settings
