"""
Optimus Research Center - Configuration Module
Independent and modular configuration.
Explicitly detached from Digital Optimus and external XAMPP services.
"""

import os
from pathlib import Path
from dotenv import load_dotenv

# Load .env if present
load_dotenv()


class Settings:
    PROJECT_NAME: str = os.getenv("PROJECT_NAME", "Optimus Research Center")
    VERSION: str = os.getenv("VERSION", "1.1.0")
    PACKAGER_VERSION: str = os.getenv("PACKAGER_VERSION", "1.0.0-m22d")
    PROTOCOL_VERSION: str = os.getenv("PROTOCOL_VERSION", "1.0.0")
    ENVIRONMENT: str = os.getenv("ENVIRONMENT", "development")
    HOST: str = os.getenv("HOST", "127.0.0.1")
    PORT: int = int(os.getenv("PORT", "8550"))

    # Paths
    BASE_DIR: Path = Path(__file__).resolve().parent.parent
    BASE_STORAGE_PATH: str = os.getenv("BASE_STORAGE_PATH", "storage")

    # Database
    DATABASE_URL: str = os.getenv("DATABASE_URL", "sqlite:///storage/db/research_center.db")

    # Security & Sandbox Limits
    SANDBOX_TIMEOUT_SECONDS: int = int(os.getenv("SANDBOX_TIMEOUT_SECONDS", "120"))
    MAX_MEMORY_MB: int = int(os.getenv("MAX_MEMORY_MB", "2048"))
    ALLOW_ARBITRARY_CODE: bool = os.getenv("ALLOW_ARBITRARY_CODE", "false").lower() == "true"

    # Optional External AI Provider
    AI_PROVIDER: str = os.getenv("AI_PROVIDER", "local_heuristic")
    ALLOW_EXTERNAL_API_CALLS: bool = os.getenv("ALLOW_EXTERNAL_API_CALLS", "false").lower() == "true"
    OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY", "")
    ANTHROPIC_API_KEY: str = os.getenv("ANTHROPIC_API_KEY", "")
    GEMINI_API_KEY: str = os.getenv("GEMINI_API_KEY", "")

    # Operation Profiles: LAPTOP_STANDALONE (default), PC_INTEGRATION_READY, VPS_PREPARED
    VALID_PROFILES: tuple = ("LAPTOP_STANDALONE", "PC_INTEGRATION_READY", "VPS_PREPARED")
    _raw_profile: str = os.getenv("ORC_PROFILE", "LAPTOP_STANDALONE").upper()
    ORC_PROFILE: str = _raw_profile if _raw_profile in ("LAPTOP_STANDALONE", "PC_INTEGRATION_READY", "VPS_PREPARED") else "LAPTOP_STANDALONE"

    # External Adapters State (Strictly False by default)
    # In LAPTOP_STANDALONE, external links are unconditionally forced False
    TITAN_INTEGRATION_ENABLED: bool = (
        False if ORC_PROFILE == "LAPTOP_STANDALONE"
        else (os.getenv("TITAN_INTEGRATION_ENABLED", "false").lower() == "true")
    )
    NEUROLAB_INTEGRATION_ENABLED: bool = (
        False if ORC_PROFILE == "LAPTOP_STANDALONE"
        else (os.getenv("NEUROLAB_INTEGRATION_ENABLED", "false").lower() == "true")
    )

    # Digital Optimus Integration (Strictly False in this mission)
    DIGITAL_OPTIMUS_INTEGRATION_ENABLED: bool = False

    @property
    def storage_path(self) -> Path:
        p = self.BASE_DIR / self.BASE_STORAGE_PATH
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def db_path(self) -> Path:
        test_override = os.getenv("ORC_TEST_DB_PATH")
        if test_override:
            return Path(test_override)
        p = self.storage_path / "db"
        p.mkdir(parents=True, exist_ok=True)
        return p / "research_center.db"

    @property
    def artifacts_path(self) -> Path:
        p = self.storage_path / "artifacts"
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def datasets_path(self) -> Path:
        p = self.storage_path / "datasets"
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def reports_path(self) -> Path:
        p = self.storage_path / "reports"
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def sandbox_tmp_path(self) -> Path:
        p = self.storage_path / "sandbox_tmp"
        p.mkdir(parents=True, exist_ok=True)
        return p

    @property
    def evidence_path(self) -> Path:
        p = self.storage_path / "evidence"
        p.mkdir(parents=True, exist_ok=True)
        return p


settings = Settings()
