from __future__ import annotations

import os
from dataclasses import dataclass


@dataclass(frozen=True)
class AppConfig:
    db_path: str
    metric_nodes_json_path: str
    offline_threshold_seconds: int
    online_threshold_seconds: int
    scheduler_interval_seconds: int
    node_id_prefix: str
    auth_mode: str


def _get_int_env(name: str, default: int) -> int:
    raw = os.environ.get(name)
    if raw is None or raw.strip() == "":
        return default
    try:
        return int(raw)
    except ValueError as exc:
        raise ValueError(f"Environment variable {name} must be an integer, got {raw!r}") from exc


def load_config() -> AppConfig:
    """读取环境变量生成配置对象，方便统一管理运行参数。"""
    return AppConfig(
        db_path=os.environ.get("DB_PATH", "/private/argus/master/db.sqlite3"),
        metric_nodes_json_path=os.environ.get(
            "METRIC_NODES_JSON_PATH", "/private/argus/metric/prometheus/nodes.json"
        ),
        offline_threshold_seconds=_get_int_env("OFFLINE_THRESHOLD_SECONDS", 180),
        online_threshold_seconds=_get_int_env("ONLINE_THRESHOLD_SECONDS", 120),
        scheduler_interval_seconds=_get_int_env("SCHEDULER_INTERVAL_SECONDS", 30),
        node_id_prefix=os.environ.get("NODE_ID_PREFIX", "A"),
        auth_mode=os.environ.get("AUTH_MODE", "disabled"),
    )
