"""Typed response models for Telegram Gateway API."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any


@dataclass(frozen=True)
class DeliveryStatus:
    status: str
    updated_at: int

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> "DeliveryStatus":
        return cls(
            status=data["status"],
            updated_at=int(data["updated_at"]),
        )


@dataclass(frozen=True)
class VerificationStatus:
    status: str
    updated_at: int
    code_entered: str | None = None

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> "VerificationStatus":
        return cls(
            status=data["status"],
            updated_at=int(data["updated_at"]),
            code_entered=data.get("code_entered"),
        )


@dataclass(frozen=True)
class RequestStatus:
    request_id: str
    phone_number: str
    request_cost: float
    is_refunded: bool | None = None
    remaining_balance: float | None = None
    delivery_status: DeliveryStatus | None = None
    verification_status: VerificationStatus | None = None
    payload: str | None = None

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> "RequestStatus":
        delivery_status_raw = data.get("delivery_status")
        verification_status_raw = data.get("verification_status")

        return cls(
            request_id=data["request_id"],
            phone_number=data["phone_number"],
            request_cost=float(data["request_cost"]),
            is_refunded=data.get("is_refunded"),
            remaining_balance=(
                float(data["remaining_balance"])
                if data.get("remaining_balance") is not None
                else None
            ),
            delivery_status=(
                DeliveryStatus.from_dict(delivery_status_raw)
                if isinstance(delivery_status_raw, dict)
                else None
            ),
            verification_status=(
                VerificationStatus.from_dict(verification_status_raw)
                if isinstance(verification_status_raw, dict)
                else None
            ),
            payload=data.get("payload"),
        )
