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

from ...core.client_wrapper import SyncClientWrapper
from .raw_client import RawEvidenceClient
import typing
from ...core.request_options import RequestOptions
from ...core.pagination import SyncPager
from ...types.dispute_evidence import DisputeEvidence
from ...core.jsonable_encoder import jsonable_encoder
from ...types.list_dispute_evidence_response import ListDisputeEvidenceResponse
from ...core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ...core.api_error import ApiError
from ...types.get_dispute_evidence_response import GetDisputeEvidenceResponse
from ...types.delete_dispute_evidence_response import DeleteDisputeEvidenceResponse
from ...core.client_wrapper import AsyncClientWrapper
from .raw_client import AsyncRawEvidenceClient
from ...core.pagination import AsyncPager


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

    @property
    def with_raw_response(self) -> RawEvidenceClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawEvidenceClient
        """
        return self._raw_client

    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

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        response = client.disputes.evidence.list(
            dispute_id="dispute_id",
        )
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        _response = self._raw_client._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(),
                    ),
                )
                _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,
                )
                _items = _parsed_response.evidence
                return SyncPager(has_next=_has_next, items=_items, get_next=_get_next)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> 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
        -------
        GetDisputeEvidenceResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.disputes.evidence.get(
            dispute_id="dispute_id",
            evidence_id="evidence_id",
        )
        """
        response = self._raw_client.get(dispute_id, evidence_id, request_options=request_options)
        return response.data

    def delete(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> 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
        -------
        DeleteDisputeEvidenceResponse
            Success

        Examples
        --------
        from square import Square

        client = Square(
            token="YOUR_TOKEN",
        )
        client.disputes.evidence.delete(
            dispute_id="dispute_id",
            evidence_id="evidence_id",
        )
        """
        response = self._raw_client.delete(dispute_id, evidence_id, request_options=request_options)
        return response.data


class AsyncEvidenceClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawEvidenceClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawEvidenceClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawEvidenceClient
        """
        return self._raw_client

    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

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            response = await client.disputes.evidence.list(
                dispute_id="dispute_id",
            )
            async for item in response:
                yield item
            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page


        asyncio.run(main())
        """
        _response = await self._raw_client._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(),
                    ),
                )
                _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,
                )
                _items = _parsed_response.evidence
                return AsyncPager(has_next=_has_next, items=_items, get_next=_get_next)
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> 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
        -------
        GetDisputeEvidenceResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.disputes.evidence.get(
                dispute_id="dispute_id",
                evidence_id="evidence_id",
            )


        asyncio.run(main())
        """
        response = await self._raw_client.get(dispute_id, evidence_id, request_options=request_options)
        return response.data

    async def delete(
        self, dispute_id: str, evidence_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> 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
        -------
        DeleteDisputeEvidenceResponse
            Success

        Examples
        --------
        import asyncio

        from square import AsyncSquare

        client = AsyncSquare(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.disputes.evidence.delete(
                dispute_id="dispute_id",
                evidence_id="evidence_id",
            )


        asyncio.run(main())
        """
        response = await self._raw_client.delete(dispute_id, evidence_id, request_options=request_options)
        return response.data
