from __future__ import annotations

from dataclasses import dataclass


@dataclass(frozen=True)
class AgentConfig:
    backend_base_url: str
    device_type: str
    app_version: str
    heartbeat_interval_sec: int = 30
    reconnect_initial_sec: int = 2
    reconnect_max_sec: int = 60
    reconnect_multiplier: float = 2.0
    update_channel: str = "stable"
    signed_bootstrap_required: bool = True

    @classmethod
    def from_dict(cls, data: dict[str, object]) -> "AgentConfig":
        backend_base_url = str(data.get("backend_base_url", "")).strip()
        device_type = str(data.get("device_type", "")).strip().lower()
        app_version = str(data.get("app_version", "")).strip()

        if not backend_base_url:
            raise ValueError("backend_base_url is required")
        if device_type not in {"desktop", "server"}:
            raise ValueError("device_type must be desktop or server")
        if not app_version:
            raise ValueError("app_version is required")

        return cls(
            backend_base_url=backend_base_url,
            device_type=device_type,
            app_version=app_version,
            heartbeat_interval_sec=int(data.get("heartbeat_interval_sec", 30)),
            reconnect_initial_sec=int(data.get("reconnect_initial_sec", 2)),
            reconnect_max_sec=int(data.get("reconnect_max_sec", 60)),
            reconnect_multiplier=float(data.get("reconnect_multiplier", 2.0)),
            update_channel=str(data.get("update_channel", "stable")),
            signed_bootstrap_required=bool(data.get("signed_bootstrap_required", True)),
        )
