import os
from dataclasses import dataclass, field
from typing import Dict


@dataclass
class Config:
    # Database
    DB_HOST: str = os.getenv("POSTGRES_HOST", "fr24-postgres")
    DB_PORT: int = int(os.getenv("POSTGRES_PORT", "5432"))
    DB_NAME: str = os.getenv("POSTGRES_DB", "fr24")
    DB_USER: str = os.getenv("POSTGRES_USER", "fr24")
    DB_PASSWORD: str = os.getenv("POSTGRES_PASSWORD", "change-me")

    # API keys
    YANDEX_RASP_API_KEY: str = os.getenv("YANDEX_RASP_API_KEY", "")

    # Airports: IATA → metadata
    AIRPORTS: Dict = field(default_factory=lambda: {
        "SVO": {"iata": "SVO", "icao": "UUEE", "yandex_code": "s9600213", "name": "Шереметьево"},
        "DME": {"iata": "DME", "icao": "UUDD", "yandex_code": "s9600366", "name": "Домодедово"},
        "VKO": {"iata": "VKO", "icao": "UUWW", "yandex_code": "s9600215", "name": "Внуково"},
        "ZIA": {"iata": "ZIA", "icao": "UUBW", "yandex_code": "s9850865", "name": "Жуковский"},
    })

    # Rate limits (seconds between requests)
    YANDEX_RATE_LIMIT_SEC: float = float(os.getenv("YANDEX_RATE_LIMIT_SEC", "1.0"))

    # Retention
    RETENTION_DAYS: int = int(os.getenv("SCHEDULE_RETENTION_DAYS", "1095"))

    # Cron schedule (UTC)
    DAILY_RUN_HOUR: int = 2
    DAILY_RUN_MINUTE: int = 0

    @property
    def DB_DSN(self) -> str:
        return (
            f"host={self.DB_HOST} port={self.DB_PORT} "
            f"dbname={self.DB_NAME} user={self.DB_USER} "
            f"password={self.DB_PASSWORD}"
        )


config = Config()
