"""Result of a heartbeat check-in request."""

from __future__ import annotations

import time
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any


@dataclass(frozen=True)
class HeartbeatResult:
    """Result of a heartbeat check-in request."""

    activation: dict[str, Any]
    next_check_in_at: str

    def get_next_check_in(self) -> datetime:
        """Get the next check-in time as a datetime object (UTC)."""
        # Parse ISO 8601 string
        dt_str = self.next_check_in_at
        # Handle timezone offset format (+00:00)
        return datetime.fromisoformat(dt_str)

    def get_seconds_until_next_check_in(self) -> float:
        """Get the number of seconds until the next required check-in.

        Returns 0 if the next check-in is in the past.
        """
        next_check_in = self.get_next_check_in()
        now = datetime.now(timezone.utc)
        delta = (next_check_in - now).total_seconds()
        return max(0.0, delta)

    def to_dict(self) -> dict[str, Any]:
        """Serialize to a plain dictionary."""
        return {
            "activation": self.activation,
            "next_check_in_at": self.next_check_in_at,
        }
