import os
from dataclasses import dataclass


@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")

    # FR24 API
    FR24_API_KEY: str = os.getenv("FR24_API_KEY", "")
    FR24_API_BASE: str = "https://fr24api.flightradar24.com"

    # Airports to track (comma-separated IATA codes)
    AIRPORTS: str = os.getenv("FR24_AIRPORTS", "SVO,DME,VKO,ZIA")

    # Airport direction prefix: "both:" means inbound+outbound
    AIRPORT_DIRECTION_PREFIX: str = os.getenv("FR24_AIRPORT_DIR_PREFIX", "both:")

    # Rate limit: 10 req/min for Explorer tier → 6s between requests
    RATE_LIMIT_SEC: float = float(os.getenv("FR24_RATE_LIMIT_SEC", "6.0"))

    # Pagination page size (for /full endpoint max 20000)
    PAGE_SIZE: int = int(os.getenv("FR24_PAGE_SIZE", "20000"))

    # Whether to fetch tracks after summaries (costs extra credits)
    FETCH_TRACKS: bool = os.getenv("FR24_FETCH_TRACKS", "false").lower() == "true"

    # Max pages to paginate through (safety cap). 200 × 20 = 4000 flights max
    MAX_PAGES: int = int(os.getenv("FR24_MAX_PAGES", "200"))

    # Credit guard: warn if estimated max flights exceeds this threshold
    CREDIT_GUARD_MAX_FLIGHTS: int = int(os.getenv("FR24_CREDIT_GUARD", "2000"))

    @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()
