"""Result of a license activation request."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Any


@dataclass(frozen=True)
class ActivationResult:
    """Result of a license activation request.

    If the API returns an error (e.g. limit reached), an ``ApiError`` is raised
    instead -- this class is only constructed on success.
    """

    activation: dict[str, Any]
    license: dict[str, Any]

    def is_activated(self) -> bool:
        """Whether the activation was successful."""
        return self.activation.get("status") == "active"

    def get_remaining_activations(self) -> int | None:
        """Get the number of remaining activations, or None if no limit is set."""
        limit = self.license.get("activation_limit")
        if limit is None:
            return None
        count = self.license.get("activation_count", 0)
        return max(0, limit - count)

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