# 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 .types.get_accounts_id_companies_response import GetAccountsIdCompaniesResponse
from .types.get_accounts_id_locations_response import GetAccountsIdLocationsResponse
from .types.get_accounts_root_response import GetAccountsRootResponse


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

    def get_accounts_root(
        self,
        *,
        search: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[GetAccountsRootResponse]:
        """
        Returns a paginated list of Ottimate accounts that the authenticated user has access to, based on partner associations linked to the OAuth client credentials.

        Parameters
        ----------
        search : typing.Optional[str]
            Search term to filter accounts by name

        limit : typing.Optional[int]
            Number of items per page

        page : typing.Optional[int]
            Page number for pagination

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

        Returns
        -------
        HttpResponse[GetAccountsRootResponse]
            Successful response with paginated account list
        """
        _response = self._client_wrapper.httpx_client.request(
            "accounts",
            method="GET",
            params={
                "search": search,
                "limit": limit,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetAccountsRootResponse,
                    parse_obj_as(
                        type_=GetAccountsRootResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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_accounts_id_companies(
        self,
        id: str,
        *,
        search: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[GetAccountsIdCompaniesResponse]:
        """
        Returns a paginated list of companies associated with the specified account.

        Parameters
        ----------
        id : str
            Account ID

        search : typing.Optional[str]
            Search term for company name

        limit : typing.Optional[int]
            Number of items per page

        page : typing.Optional[int]
            Page number for pagination

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

        Returns
        -------
        HttpResponse[GetAccountsIdCompaniesResponse]
            Successful response with paginated company list
        """
        _response = self._client_wrapper.httpx_client.request(
            f"accounts/{jsonable_encoder(id)}/companies",
            method="GET",
            params={
                "search": search,
                "limit": limit,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetAccountsIdCompaniesResponse,
                    parse_obj_as(
                        type_=GetAccountsIdCompaniesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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_accounts_id_locations(
        self,
        id: str,
        *,
        search: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[GetAccountsIdLocationsResponse]:
        """
        Returns a paginated list of locations associated with the specified account.

        Parameters
        ----------
        id : str
            Account ID

        search : typing.Optional[str]
            Search term for location name

        limit : typing.Optional[int]
            Number of items per page

        page : typing.Optional[int]
            Page number for pagination

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

        Returns
        -------
        HttpResponse[GetAccountsIdLocationsResponse]
            Successful response with paginated location list
        """
        _response = self._client_wrapper.httpx_client.request(
            f"accounts/{jsonable_encoder(id)}/locations",
            method="GET",
            params={
                "search": search,
                "limit": limit,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetAccountsIdLocationsResponse,
                    parse_obj_as(
                        type_=GetAccountsIdLocationsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 AsyncRawAccountsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get_accounts_root(
        self,
        *,
        search: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[GetAccountsRootResponse]:
        """
        Returns a paginated list of Ottimate accounts that the authenticated user has access to, based on partner associations linked to the OAuth client credentials.

        Parameters
        ----------
        search : typing.Optional[str]
            Search term to filter accounts by name

        limit : typing.Optional[int]
            Number of items per page

        page : typing.Optional[int]
            Page number for pagination

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

        Returns
        -------
        AsyncHttpResponse[GetAccountsRootResponse]
            Successful response with paginated account list
        """
        _response = await self._client_wrapper.httpx_client.request(
            "accounts",
            method="GET",
            params={
                "search": search,
                "limit": limit,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetAccountsRootResponse,
                    parse_obj_as(
                        type_=GetAccountsRootResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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_accounts_id_companies(
        self,
        id: str,
        *,
        search: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[GetAccountsIdCompaniesResponse]:
        """
        Returns a paginated list of companies associated with the specified account.

        Parameters
        ----------
        id : str
            Account ID

        search : typing.Optional[str]
            Search term for company name

        limit : typing.Optional[int]
            Number of items per page

        page : typing.Optional[int]
            Page number for pagination

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

        Returns
        -------
        AsyncHttpResponse[GetAccountsIdCompaniesResponse]
            Successful response with paginated company list
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"accounts/{jsonable_encoder(id)}/companies",
            method="GET",
            params={
                "search": search,
                "limit": limit,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetAccountsIdCompaniesResponse,
                    parse_obj_as(
                        type_=GetAccountsIdCompaniesResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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_accounts_id_locations(
        self,
        id: str,
        *,
        search: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[GetAccountsIdLocationsResponse]:
        """
        Returns a paginated list of locations associated with the specified account.

        Parameters
        ----------
        id : str
            Account ID

        search : typing.Optional[str]
            Search term for location name

        limit : typing.Optional[int]
            Number of items per page

        page : typing.Optional[int]
            Page number for pagination

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

        Returns
        -------
        AsyncHttpResponse[GetAccountsIdLocationsResponse]
            Successful response with paginated location list
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"accounts/{jsonable_encoder(id)}/locations",
            method="GET",
            params={
                "search": search,
                "limit": limit,
                "page": page,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetAccountsIdLocationsResponse,
                    parse_obj_as(
                        type_=GetAccountsIdLocationsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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)
