"""
Sofiia Supervisor — Configuration

All settings from environment variables with sane defaults.
"""

from __future__ import annotations
import os
from typing import Optional


class Settings:
    # ─── Router / Gateway ────────────────────────────────────────────────────
    # URL of the DAARION router service that exposes /v1/tools/execute
    GATEWAY_BASE_URL: str = os.getenv("GATEWAY_BASE_URL", "http://router:8000")
    SUPERVISOR_API_KEY: str = os.getenv("SUPERVISOR_API_KEY", "")

    # ─── State backend ───────────────────────────────────────────────────────
    STATE_BACKEND: str = os.getenv("SUPERVISOR_STATE_BACKEND", "redis")  # redis | memory
    REDIS_URL: str = os.getenv("REDIS_URL", "redis://redis:6379/0")
    RUN_TTL_SEC: int = int(os.getenv("RUN_TTL_SEC", "86400"))  # 24h

    # ─── Supervisor API ──────────────────────────────────────────────────────
    SUPERVISOR_HOST: str = os.getenv("SUPERVISOR_HOST", "0.0.0.0")
    SUPERVISOR_PORT: int = int(os.getenv("SUPERVISOR_PORT", "8080"))
    # Optional API key to protect supervisor HTTP endpoints (independent of gateway RBAC)
    SUPERVISOR_INTERNAL_KEY: str = os.getenv("SUPERVISOR_INTERNAL_KEY", "")

    # ─── Agent defaults ──────────────────────────────────────────────────────
    DEFAULT_AGENT_ID: str = os.getenv("DEFAULT_AGENT_ID", "sofiia")
    DEFAULT_WORKSPACE_ID: str = os.getenv("DEFAULT_WORKSPACE_ID", "daarion")
    DEFAULT_TIMEZONE: str = os.getenv("DEFAULT_TIMEZONE", "Europe/Kiev")

    # ─── Timeouts ────────────────────────────────────────────────────────────
    # Per gateway tool call (seconds)
    TOOL_CALL_TIMEOUT_SEC: float = float(os.getenv("TOOL_CALL_TIMEOUT_SEC", "60.0"))
    # Max retries for retryable errors (2xx vs 5xx)
    TOOL_CALL_MAX_RETRIES: int = int(os.getenv("TOOL_CALL_MAX_RETRIES", "2"))
    # Polling interval for async jobs (seconds)
    JOB_POLL_INTERVAL_SEC: float = float(os.getenv("JOB_POLL_INTERVAL_SEC", "3.0"))
    # Max job wait time (seconds) — safety valve
    JOB_MAX_WAIT_SEC: float = float(os.getenv("JOB_MAX_WAIT_SEC", "300.0"))

    # ─── Incident triage constraints ─────────────────────────────────────────
    INCIDENT_MAX_TIME_WINDOW_H: int = int(os.getenv("INCIDENT_MAX_TIME_WINDOW_H", "24"))
    INCIDENT_MAX_LOG_LINES: int = int(os.getenv("INCIDENT_MAX_LOG_LINES", "200"))


settings = Settings()
