"""Global configuration settings for SD-API."""

from __future__ import annotations

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


@dataclass
class Settings:
    """Global settings for SD-API SDK.

    Can be overridden via environment variables or by setting attributes at runtime.

    Attributes
    ----------
    default_timeout : float
        Default HTTP request timeout in seconds (default: 30).
    max_retries : int
        Maximum number of retries on transient HTTP errors (default: 3).
    default_limit : int
        Default number of records to fetch (default: 100).
    log_level : str
        Logging level: DEBUG, INFO, WARNING, ERROR (default: INFO).
    sse_reconnect_delay : float
        Seconds to wait before reconnecting on SSE stream error (default: 2.0).
    poll_interval : float
        Default polling interval in seconds when not using SSE (default: 1.0).
    """

    default_timeout: float = 30.0
    max_retries: int = 3
    default_limit: int = 100
    log_level: str = "INFO"
    sse_reconnect_delay: float = 2.0
    poll_interval: float = 1.0

    # Shanghai (UTC+8) timezone identifier
    shanghai_tz: str = "Asia/Shanghai"


# Global singleton instance — reads from environment on import
def _load_settings() -> Settings:
    s = Settings()
    s.default_timeout = float(
        os.environ.get("SD_API_TIMEOUT", s.default_timeout)
    )
    s.max_retries = int(os.environ.get("SD_API_MAX_RETRIES", s.max_retries))
    s.default_limit = int(os.environ.get("SD_API_LIMIT", s.default_limit))
    s.log_level = os.environ.get("SD_API_LOG_LEVEL", s.log_level)
    s.sse_reconnect_delay = float(
        os.environ.get("SD_API_SSE_DELAY", s.sse_reconnect_delay)
    )
    s.poll_interval = float(
        os.environ.get("SD_API_POLL_INTERVAL", s.poll_interval)
    )
    return s


settings: Settings = _load_settings()
