import os
from dataclasses import dataclass


def _bool_env(name: str, default: bool) -> bool:
    value = os.getenv(name)
    if value is None:
        return default
    return value.strip().lower() in {"1", "true", "yes", "on"}


def _int_env(name: str, default: int) -> int:
    try:
        return int(os.getenv(name, str(default)))
    except (TypeError, ValueError):
        return default


def _float_env(name: str, default: float) -> float:
    try:
        return float(os.getenv(name, str(default)))
    except (TypeError, ValueError):
        return default


def _str_env(name: str, default: str) -> str:
    value = os.getenv(name)
    if value is None:
        return default
    return value.strip() or default


def _optional_env(name: str) -> str | None:
    value = os.getenv(name)
    if value is None:
        return None
    value = value.strip()
    return value or None


@dataclass(frozen=True)
class RiskConfig:
    cors_allow_origins: tuple[str, ...]
    session_cookie_name: str
    session_cookie_secure: bool
    session_cookie_samesite: str
    session_cookie_domain: str | None
    session_cookie_max_age: int
    session_cookie_path: str
    session_rotation_seconds: int
    api_window_seconds: int
    api_soft_limit_per_window: int
    api_hard_limit_per_window: int
    llm_window_seconds: int
    llm_soft_limit_per_window: int
    llm_hard_limit_per_window: int
    session_concurrency_limit: int
    global_concurrency_limit: int
    daily_budget_global_usd: float
    daily_budget_session_usd: float
    daily_budget_ip_usd: float
    single_request_max_cost_usd: float
    delay_step_ms: int
    delay_cap_ms: int
    model_circuit_breaker_failures: int
    model_circuit_ttl_seconds: int
    enforce_redis_fail_closed: bool
    completion_model: str
    pro_model: str
    vision_model: str
    completion_max_input_chars: int
    completion_max_output_tokens: int
    completion_temperature: float
    pro_max_input_chars: int
    pro_max_output_tokens: int
    pro_temperature: float
    web_search_model: str
    speech_tts_model: str
    speech_asr_model: str
    web_search_max_input_chars: int
    web_search_max_output_tokens: int
    web_search_temperature: float
    compress_max_input_chars: int
    compress_max_output_tokens: int
    ocr_max_input_bytes: int
    speech_tts_max_input_chars: int
    speech_asr_max_input_bytes: int
    completion_input_cost_per_1k: float
    completion_output_cost_per_1k: float
    pro_input_cost_per_1k: float
    pro_output_cost_per_1k: float
    vision_input_cost_per_1k: float
    vision_output_cost_per_1k: float
    speech_tts_input_cost_per_1k_chars: float
    speech_tts_output_cost_per_minute_audio: float
    speech_asr_input_cost_per_mb: float


def load_risk_config() -> RiskConfig:
    raw_origins = _str_env(
        "CORS_ALLOW_ORIGINS",
        "https://chat.imageteach.tech,http://localhost:8080,http://127.0.0.1:8080,http://localhost:5173,http://127.0.0.1:5173",
    )
    cors_allow_origins = tuple(
        origin.strip() for origin in raw_origins.split(",") if origin.strip()
    )
    return RiskConfig(
        cors_allow_origins=cors_allow_origins,
        session_cookie_name=_str_env("SESSION_COOKIE_NAME", "llm_anonymous_session"),
        session_cookie_secure=_bool_env("SESSION_COOKIE_SECURE", True),
        session_cookie_samesite=_str_env("SESSION_COOKIE_SAMESITE", "none"),
        session_cookie_domain=_optional_env("SESSION_COOKIE_DOMAIN"),
        session_cookie_max_age=_int_env("SESSION_COOKIE_MAX_AGE_SECONDS", 60 * 60 * 24 * 30),
        session_cookie_path=_str_env("SESSION_COOKIE_PATH", "/"),
        session_rotation_seconds=_int_env("SESSION_ROTATION_SECONDS", 60 * 60 * 24),
        api_window_seconds=_int_env("RISK_API_WINDOW_SECONDS", 60),
        api_soft_limit_per_window=_int_env("RISK_API_SOFT_LIMIT", 90),
        api_hard_limit_per_window=_int_env("RISK_API_HARD_LIMIT", 180),
        llm_window_seconds=_int_env("RISK_LLM_WINDOW_SECONDS", 600),
        llm_soft_limit_per_window=_int_env("RISK_LLM_SOFT_LIMIT", 8),
        llm_hard_limit_per_window=_int_env("RISK_LLM_HARD_LIMIT", 16),
        session_concurrency_limit=_int_env("RISK_SESSION_CONCURRENCY_LIMIT", 2),
        global_concurrency_limit=_int_env("RISK_GLOBAL_CONCURRENCY_LIMIT", 12),
        daily_budget_global_usd=_float_env("RISK_DAILY_BUDGET_GLOBAL_USD", 20.0),
        daily_budget_session_usd=_float_env("RISK_DAILY_BUDGET_SESSION_USD", 2.0),
        daily_budget_ip_usd=_float_env("RISK_DAILY_BUDGET_IP_USD", 5.0),
        single_request_max_cost_usd=_float_env("RISK_SINGLE_REQUEST_MAX_COST_USD", 0.8),
        delay_step_ms=_int_env("RISK_DELAY_STEP_MS", 2500),
        delay_cap_ms=_int_env("RISK_DELAY_CAP_MS", 30000),
        model_circuit_breaker_failures=_int_env("RISK_MODEL_CIRCUIT_FAILURES", 8),
        model_circuit_ttl_seconds=_int_env("RISK_MODEL_CIRCUIT_TTL_SECONDS", 300),
        enforce_redis_fail_closed=_bool_env("RISK_ENFORCE_REDIS_FAIL_CLOSED", False),
        completion_model=_str_env("RISK_COMPLETION_MODEL", os.getenv("LLM_MODEL", "Nex-N2-mini-mlx-OptiQ-8bit-MTP")),
        pro_model=_str_env("RISK_PRO_MODEL", os.getenv("PRO_LLM_MODEL", os.getenv("LLM_MODEL", "Nex-N2-mini-mlx-OptiQ-8bit-MTP"))),
        vision_model=_str_env("RISK_VISION_MODEL", os.getenv("VLM_MODEL", "Nex-N2-mini-mlx-OptiQ-8bit-MTP")),
        completion_max_input_chars=_int_env("RISK_COMPLETION_MAX_INPUT_CHARS", 24000),
        completion_max_output_tokens=_int_env("RISK_COMPLETION_MAX_OUTPUT_TOKENS", 768),
        completion_temperature=_float_env("RISK_COMPLETION_TEMPERATURE", 0.4),
        pro_max_input_chars=_int_env("RISK_PRO_MAX_INPUT_CHARS", 48000),
        pro_max_output_tokens=_int_env("RISK_PRO_MAX_OUTPUT_TOKENS", 2048),
        pro_temperature=_float_env("RISK_PRO_TEMPERATURE", 0.6),
        web_search_model=_str_env("RISK_WEB_SEARCH_MODEL", os.getenv("LLM_MODEL", "Nex-N2-mini-mlx-OptiQ-8bit-MTP")),
        speech_tts_model=_str_env("RISK_SPEECH_TTS_MODEL", "Qwen3-TTS-12Hz-1.7B-VoiceDesign-8bit"),
        speech_asr_model=_str_env("RISK_SPEECH_ASR_MODEL", "Qwen3-ASR-0.6B-8bit"),
        web_search_max_input_chars=_int_env("RISK_WEB_SEARCH_MAX_INPUT_CHARS", 128000),
        web_search_max_output_tokens=_int_env("RISK_WEB_SEARCH_MAX_OUTPUT_TOKENS", 4096),
        web_search_temperature=_float_env("RISK_WEB_SEARCH_TEMPERATURE", 0.4),
        compress_max_input_chars=_int_env("RISK_COMPRESS_MAX_INPUT_CHARS", 128000),
        compress_max_output_tokens=_int_env("RISK_COMPRESS_MAX_OUTPUT_TOKENS", 1536),
        ocr_max_input_bytes=_int_env("RISK_OCR_MAX_INPUT_BYTES", 100 * 1024 * 1024),
        speech_tts_max_input_chars=_int_env("RISK_SPEECH_TTS_MAX_INPUT_CHARS", 4096),
        speech_asr_max_input_bytes=_int_env("RISK_SPEECH_ASR_MAX_INPUT_BYTES", 100 * 1024 * 1024),
        completion_input_cost_per_1k=_float_env("RISK_COMPLETION_INPUT_COST_PER_1K", 0.0004),
        completion_output_cost_per_1k=_float_env("RISK_COMPLETION_OUTPUT_COST_PER_1K", 0.0016),
        pro_input_cost_per_1k=_float_env("RISK_PRO_INPUT_COST_PER_1K", 0.003),
        pro_output_cost_per_1k=_float_env("RISK_PRO_OUTPUT_COST_PER_1K", 0.012),
        vision_input_cost_per_1k=_float_env("RISK_VISION_INPUT_COST_PER_1K", 0.0008),
        vision_output_cost_per_1k=_float_env("RISK_VISION_OUTPUT_COST_PER_1K", 0.0024),
        speech_tts_input_cost_per_1k_chars=_float_env("RISK_SPEECH_TTS_INPUT_COST_PER_1K_CHARS", 0.0),
        speech_tts_output_cost_per_minute_audio=_float_env("RISK_SPEECH_TTS_OUTPUT_COST_PER_MINUTE_AUDIO", 0.0),
        speech_asr_input_cost_per_mb=_float_env("RISK_SPEECH_ASR_INPUT_COST_PER_MB", 0.0),
    )
