"""TypedDict definitions matching the LicensesOS API response shapes."""

from __future__ import annotations

from typing import Any, Optional, TypedDict


# ---------------------------------------------------------------------------
# API envelope
# ---------------------------------------------------------------------------


class ApiErrorBody(TypedDict):
    code: str
    message: str


class ApiEnvelope(TypedDict, total=False):
    success: bool
    data: Any
    message: str
    error: ApiErrorBody
    errors: dict[str, list[str]]


# ---------------------------------------------------------------------------
# Validation
# ---------------------------------------------------------------------------


class ValidationLicenseData(TypedDict):
    id: str
    status: str
    entitlements: Optional[dict[str, Any]]
    expires_at: Optional[str]
    is_expired: bool
    activation_limit: Optional[int]
    activation_count: int
    uses_count: Optional[int]
    max_uses: Optional[int]
    uses_remaining: Optional[int]
    usage_reset_interval: Optional[str]
    uses_reset_at: Optional[str]


class ValidationLicenseStatusOnly(TypedDict):
    status: str


class ValidationActivationData(TypedDict):
    id: str
    identifier: str
    last_seen_at: Optional[str]


class ValidationErrorData(TypedDict):
    code: str
    message: str


class ValidationResponseData(TypedDict, total=False):
    valid: bool
    license: ValidationLicenseData | ValidationLicenseStatusOnly
    activated: bool
    activation: ValidationActivationData
    offline_token: str
    error: ValidationErrorData


# ---------------------------------------------------------------------------
# Activation
# ---------------------------------------------------------------------------


class ActivationDetailData(TypedDict):
    id: str
    identifier: str
    label: Optional[str]
    status: str
    activated_at: str
    last_seen_at: Optional[str]
    metadata: Optional[dict[str, Any]]


class ActivationLicenseData(TypedDict):
    id: str
    status: str
    entitlements: Optional[dict[str, Any]]
    expires_at: Optional[str]
    activation_count: int
    activation_limit: Optional[int]


class ActivationResponseData(TypedDict):
    activation: ActivationDetailData
    license: ActivationLicenseData


# ---------------------------------------------------------------------------
# Deactivation
# ---------------------------------------------------------------------------


class DeactivationResponseData(TypedDict):
    deactivated: bool


# ---------------------------------------------------------------------------
# List Activations
# ---------------------------------------------------------------------------


class ActivationsResponseData(TypedDict):
    activations: list[ActivationDetailData]
    count: int
    limit: Optional[int]


# ---------------------------------------------------------------------------
# Heartbeat
# ---------------------------------------------------------------------------


class HeartbeatResponseData(TypedDict):
    activation: ActivationDetailData
    next_check_in_at: str


# ---------------------------------------------------------------------------
# Usage Metering
# ---------------------------------------------------------------------------


class UsageData(TypedDict):
    uses_count: int
    max_uses: Optional[int]
    uses_remaining: Optional[int]
    usage_reset_interval: Optional[str]
    uses_reset_at: Optional[str]


class UsageResponseData(TypedDict):
    usage: UsageData


# ---------------------------------------------------------------------------
# Offline Token Payload
# ---------------------------------------------------------------------------


class OfflineTokenPayload(TypedDict, total=False):
    license_id: str
    status: str
    entitlements: Optional[dict[str, Any]]
    expires_at: Optional[str]
    activation_limit: Optional[int]
    activation_count: int
    identifier: Optional[str]
    activated: bool
    uses_count: int
    max_uses: Optional[int]
    iat: int
    exp: int
    ttl: int
