"""Configuration for the ClaudeTools Discord Bot."""
from pathlib import Path
from typing import Optional

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    discord_token: str = Field(..., description="Discord bot token")
    discord_guild_id: Optional[int] = Field(None, description="Discord guild/server ID")

    # Optional: leave unset to use the local Claude Code OAuth credential
    # (Pro/Max subscription). Set to use the API with metered billing.
    anthropic_api_key: Optional[str] = Field(default=None, description="Anthropic API key")
    claude_model: str = Field(default="claude-sonnet-4-6", description="Claude model")

    # Workspace the agent operates in. Default is the Windows BEAST path; override
    # via CLAUDETOOLS_ROOT env var on Mac/Linux.
    claudetools_root: Path = Field(
        default=Path("c:/Users/guru/ClaudeTools"),
        description="Path to ClaudeTools repository (agent cwd)",
    )

    # Path to Discord-specific system prompt, relative to claudetools_root.
    # Edit projects/discord-bot/DISCORD_CLAUDE.md to change bot behavior without
    # touching the main CLAUDE.md. Changes take effect on next bot restart.
    discord_system_prompt: Path = Field(
        default=Path("projects/discord-bot/DISCORD_CLAUDE.md"),
        description="System prompt for Discord bot sessions (relative to claudetools_root)",
    )

    log_level: str = Field(default="INFO", description="Logging level")
    log_file: Optional[Path] = Field(default=Path("logs/bot.log"), description="Log file")

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="ignore",
    )

    def validate_paths(self) -> None:
        if not self.claudetools_root.exists():
            raise FileNotFoundError(
                f"ClaudeTools not found at {self.claudetools_root}. "
                "Set CLAUDETOOLS_ROOT environment variable."
            )
        claude_md = self.claudetools_root / ".claude" / "CLAUDE.md"
        if not claude_md.exists():
            raise FileNotFoundError(
                f"CLAUDE.md not found at {claude_md}. "
                "Agent system prompt cannot be loaded."
            )


settings = Settings()
