"""Usage handling for the Meemo Python client.

This module provides the Usage class for accessing usage/quota data
from the Meemo External API.

Authors:
    GDP Labs

References:
    https://gdplabs.gitbook.io/meemo/resources/external-api-documentation
"""

import logging

from meemo._base import _ERR_EXPECTED_DICT, _BaseService, _BaseUsageMixin
from meemo.models import UsageResponse

logger = logging.getLogger(__name__)


class Usage(_BaseService, _BaseUsageMixin):
    """Handles Usage API operations for the Meemo External API."""

    def get_usage(
        self,
        organization_id: int | None = None,
        start_date: str | None = None,
        end_date: str | None = None,
        extra_headers: dict[str, str] | None = None,
    ) -> UsageResponse:
        """Get usage/quota information.

        Args:
            organization_id (int | None): Filter by organization.
            start_date (str | None): Start date filter (YYYY-MM-DD).
            end_date (str | None): End date filter (YYYY-MM-DD).
            extra_headers (dict[str, str] | None): Additional headers.

        Returns:
            UsageResponse: Usage records.

        Raises:
            httpx.HTTPStatusError: If the API request fails.
        """
        logger.debug("Retrieving usage data")

        url = self._usage_url()
        headers = self._prepare_headers(extra_headers)
        params = self._build_usage_params(organization_id, start_date, end_date)

        response_data = self._make_request("GET", url, headers, params=params)
        if not isinstance(response_data, dict):
            raise TypeError(_ERR_EXPECTED_DICT)
        return UsageResponse(**response_data)
