# 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.pagination import AsyncPager, BaseHttpResponse, SyncPager
from ...core.request_options import RequestOptions
from ...core.unchecked_base_model import construct_type
from ...types.delete_dispute_evidence_response import DeleteDisputeEvidenceResponse
from ...types.dispute_evidence import DisputeEvidence
from ...types.get_dispute_evidence_response import GetDisputeEvidenceResponse
from ...types.list_dispute_evidence_response import ListDisputeEvidenceResponse


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

    def list(
        self,
        dispute_id: str,
        *,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[DisputeEvidence]:
        """
        Returns a list of evidence associated with a dispute.

        Parameters
        ----------
        dispute_id : str
            The ID of the dispute.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this cursor to retrieve the next set of results for the original query.
            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

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

        Returns
        -------
        SyncPager[DisputeEvidence]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/disputes/{jsonable_encoder(dispute_id)}/evidence",
            method="GET",
            params={
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListDisputeEvidenceResponse,
                    construct_type(
                        type_=ListDisputeEvidenceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.evidence
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""
                _get_next = lambda: self.list(
                    dispute_id,
                    cursor=_parsed_next,
                    request_options=request_options,
                )
                return SyncPager(
                    has_next=_has_next, items=_items, get_next=_get_next, response=BaseHttpResponse(response=_response)
                )
            _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(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetDisputeEvidenceResponse]:
        """
        Returns the metadata for the evidence specified in the request URL path.

        You must maintain a copy of any evidence uploaded if you want to reference it later. Evidence cannot be downloaded after you upload it.

        Parameters
        ----------
        dispute_id : str
            The ID of the dispute from which you want to retrieve evidence metadata.

        evidence_id : str
            The ID of the evidence to retrieve.

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

        Returns
        -------
        HttpResponse[GetDisputeEvidenceResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/disputes/{jsonable_encoder(dispute_id)}/evidence/{jsonable_encoder(evidence_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetDisputeEvidenceResponse,
                    construct_type(
                        type_=GetDisputeEvidenceResponse,  # 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 delete(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[DeleteDisputeEvidenceResponse]:
        """
        Removes specified evidence from a dispute.
        Square does not send the bank any evidence that is removed.

        Parameters
        ----------
        dispute_id : str
            The ID of the dispute from which you want to remove evidence.

        evidence_id : str
            The ID of the evidence you want to remove.

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

        Returns
        -------
        HttpResponse[DeleteDisputeEvidenceResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/disputes/{jsonable_encoder(dispute_id)}/evidence/{jsonable_encoder(evidence_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteDisputeEvidenceResponse,
                    construct_type(
                        type_=DeleteDisputeEvidenceResponse,  # 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 AsyncRawEvidenceClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        dispute_id: str,
        *,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[DisputeEvidence]:
        """
        Returns a list of evidence associated with a dispute.

        Parameters
        ----------
        dispute_id : str
            The ID of the dispute.

        cursor : typing.Optional[str]
            A pagination cursor returned by a previous call to this endpoint.
            Provide this cursor to retrieve the next set of results for the original query.
            For more information, see [Pagination](https://developer.squareup.com/docs/build-basics/common-api-patterns/pagination).

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

        Returns
        -------
        AsyncPager[DisputeEvidence]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/disputes/{jsonable_encoder(dispute_id)}/evidence",
            method="GET",
            params={
                "cursor": cursor,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListDisputeEvidenceResponse,
                    construct_type(
                        type_=ListDisputeEvidenceResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.evidence
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""

                async def _get_next():
                    return await self.list(
                        dispute_id,
                        cursor=_parsed_next,
                        request_options=request_options,
                    )

                return AsyncPager(
                    has_next=_has_next, items=_items, get_next=_get_next, response=BaseHttpResponse(response=_response)
                )
            _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(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetDisputeEvidenceResponse]:
        """
        Returns the metadata for the evidence specified in the request URL path.

        You must maintain a copy of any evidence uploaded if you want to reference it later. Evidence cannot be downloaded after you upload it.

        Parameters
        ----------
        dispute_id : str
            The ID of the dispute from which you want to retrieve evidence metadata.

        evidence_id : str
            The ID of the evidence to retrieve.

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

        Returns
        -------
        AsyncHttpResponse[GetDisputeEvidenceResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/disputes/{jsonable_encoder(dispute_id)}/evidence/{jsonable_encoder(evidence_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetDisputeEvidenceResponse,
                    construct_type(
                        type_=GetDisputeEvidenceResponse,  # 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 delete(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[DeleteDisputeEvidenceResponse]:
        """
        Removes specified evidence from a dispute.
        Square does not send the bank any evidence that is removed.

        Parameters
        ----------
        dispute_id : str
            The ID of the dispute from which you want to remove evidence.

        evidence_id : str
            The ID of the evidence you want to remove.

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

        Returns
        -------
        AsyncHttpResponse[DeleteDisputeEvidenceResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/disputes/{jsonable_encoder(dispute_id)}/evidence/{jsonable_encoder(evidence_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    DeleteDisputeEvidenceResponse,
                    construct_type(
                        type_=DeleteDisputeEvidenceResponse,  # 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)
