# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.usage_detail_report_response import UsageDetailReportResponse
from ..types.usage_event_list_response import UsageEventListResponse


class RawAnalyticsUsageDetailClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def get_usage_detail_report(
        self,
        org_id: str,
        *,
        year: typing.Optional[int] = None,
        month: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UsageDetailReportResponse]:
        """
        Multi-dimensional cost breakdown report for an organization.

        Returns cost and usage broken down by model, API key, priority level,
        Flowplate workflow source, and daily totals for the period.

        Parameters
        ----------
        org_id : str

        year : typing.Optional[int]
            Year (defaults to current)

        month : typing.Optional[int]
            Month (defaults to current)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[UsageDetailReportResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/analytics/usage/detail/{jsonable_encoder(org_id)}",
            method="GET",
            params={
                "year": year,
                "month": month,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UsageDetailReportResponse,
                    parse_obj_as(
                        type_=UsageDetailReportResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get_usage_events(
        self,
        org_id: str,
        *,
        year: typing.Optional[int] = None,
        month: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        model: typing.Optional[str] = None,
        api_key_id: typing.Optional[str] = None,
        priority: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UsageEventListResponse]:
        """
        Paginated list of individual usage events with optional filters.

        Supports filtering by model, API key, and priority. Use limit + offset
        for pagination; the response includes total_count and has_more.

        Parameters
        ----------
        org_id : str

        year : typing.Optional[int]
            Year (defaults to current)

        month : typing.Optional[int]
            Month (defaults to current)

        limit : typing.Optional[int]
            Events per page

        offset : typing.Optional[int]
            Pagination offset

        model : typing.Optional[str]
            Filter by model identifier

        api_key_id : typing.Optional[str]
            Filter by API key ID

        priority : typing.Optional[str]
            Filter by priority: low, default, high

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[UsageEventListResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/analytics/usage/events/{jsonable_encoder(org_id)}",
            method="GET",
            params={
                "year": year,
                "month": month,
                "limit": limit,
                "offset": offset,
                "model": model,
                "api_key_id": api_key_id,
                "priority": priority,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UsageEventListResponse,
                    parse_obj_as(
                        type_=UsageEventListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawAnalyticsUsageDetailClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get_usage_detail_report(
        self,
        org_id: str,
        *,
        year: typing.Optional[int] = None,
        month: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UsageDetailReportResponse]:
        """
        Multi-dimensional cost breakdown report for an organization.

        Returns cost and usage broken down by model, API key, priority level,
        Flowplate workflow source, and daily totals for the period.

        Parameters
        ----------
        org_id : str

        year : typing.Optional[int]
            Year (defaults to current)

        month : typing.Optional[int]
            Month (defaults to current)

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[UsageDetailReportResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/analytics/usage/detail/{jsonable_encoder(org_id)}",
            method="GET",
            params={
                "year": year,
                "month": month,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UsageDetailReportResponse,
                    parse_obj_as(
                        type_=UsageDetailReportResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get_usage_events(
        self,
        org_id: str,
        *,
        year: typing.Optional[int] = None,
        month: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        model: typing.Optional[str] = None,
        api_key_id: typing.Optional[str] = None,
        priority: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UsageEventListResponse]:
        """
        Paginated list of individual usage events with optional filters.

        Supports filtering by model, API key, and priority. Use limit + offset
        for pagination; the response includes total_count and has_more.

        Parameters
        ----------
        org_id : str

        year : typing.Optional[int]
            Year (defaults to current)

        month : typing.Optional[int]
            Month (defaults to current)

        limit : typing.Optional[int]
            Events per page

        offset : typing.Optional[int]
            Pagination offset

        model : typing.Optional[str]
            Filter by model identifier

        api_key_id : typing.Optional[str]
            Filter by API key ID

        priority : typing.Optional[str]
            Filter by priority: low, default, high

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[UsageEventListResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/analytics/usage/events/{jsonable_encoder(org_id)}",
            method="GET",
            params={
                "year": year,
                "month": month,
                "limit": limit,
                "offset": offset,
                "model": model,
                "api_key_id": api_key_id,
                "priority": priority,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UsageEventListResponse,
                    parse_obj_as(
                        type_=UsageEventListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        parse_obj_as(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
