"""Result of a usage increment request."""

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import Any


@dataclass(frozen=True)
class UsageResult:
    """Result of a usage increment request."""

    usage: dict[str, Any]

    def is_limit_reached(self) -> bool:
        """Whether the usage limit has been reached."""
        max_uses = self.usage.get("max_uses")
        if max_uses is None:
            return False
        return self.usage.get("uses_count", 0) >= max_uses

    def get_usage_percentage(self) -> float | None:
        """Get the current usage as a percentage of the limit.

        Returns None if no limit is set.
        """
        max_uses = self.usage.get("max_uses")
        if max_uses is None or max_uses == 0:
            return None
        return min(100.0, (self.usage.get("uses_count", 0) / max_uses) * 100)

    def get_reset_date(self) -> datetime | None:
        """Get the next usage reset date, or None if no reset is configured."""
        reset_at = self.usage.get("uses_reset_at")
        if reset_at is None:
            return None
        return datetime.fromisoformat(reset_at)

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