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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.maybe_expanded_comment import MaybeExpandedComment
from ..core.unchecked_base_model import construct_type
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..core.jsonable_encoder import jsonable_encoder
from ..core.client_wrapper import AsyncClientWrapper

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


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

    def list(
        self,
        *,
        annotation: typing.Optional[int] = None,
        annotators: typing.Optional[str] = None,
        draft: typing.Optional[int] = None,
        expand_created_by: typing.Optional[bool] = None,
        ordering: typing.Optional[str] = None,
        projects: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[MaybeExpandedComment]:
        """
        List all comments for a specific annotation ID.

        Parameters
        ----------
        annotation : typing.Optional[int]

        annotators : typing.Optional[str]

        draft : typing.Optional[int]

        expand_created_by : typing.Optional[bool]

        ordering : typing.Optional[str]
            Which field to use when ordering the results.

        projects : typing.Optional[str]

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

        Returns
        -------
        typing.List[MaybeExpandedComment]
            List of comments

        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.comments.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/comments/",
            method="GET",
            params={
                "annotation": annotation,
                "annotators": annotators,
                "draft": draft,
                "expand_created_by": expand_created_by,
                "ordering": ordering,
                "projects": projects,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[MaybeExpandedComment],
                    construct_type(
                        type_=typing.List[MaybeExpandedComment],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 create(
        self,
        *,
        expand_created_by: typing.Optional[bool] = None,
        annotation: typing.Optional[int] = OMIT,
        classifications: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        draft: typing.Optional[int] = OMIT,
        is_resolved: typing.Optional[bool] = OMIT,
        region_ref: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        text: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MaybeExpandedComment:
        """
        Create a comment for a specific annotation ID.

        Parameters
        ----------
        expand_created_by : typing.Optional[bool]
            Expand the created_by field

        annotation : typing.Optional[int]

        classifications : typing.Optional[typing.Optional[typing.Any]]

        draft : typing.Optional[int]

        is_resolved : typing.Optional[bool]
            True if the comment is resolved

        region_ref : typing.Optional[typing.Optional[typing.Any]]

        text : typing.Optional[str]
            Reviewer or annotator comment

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

        Returns
        -------
        MaybeExpandedComment


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.comments.create()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/comments/",
            method="POST",
            params={
                "expand_created_by": expand_created_by,
            },
            json={
                "annotation": annotation,
                "classifications": classifications,
                "draft": draft,
                "is_resolved": is_resolved,
                "region_ref": region_ref,
                "text": text,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    MaybeExpandedComment,
                    construct_type(
                        type_=MaybeExpandedComment,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 export(
        self,
        *,
        annotation: typing.Optional[int] = None,
        annotators: typing.Optional[str] = None,
        draft: typing.Optional[int] = None,
        expand_created_by: typing.Optional[bool] = None,
        projects: typing.Optional[str] = None,
        tz: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Iterator[bytes]:
        """
        Export comments to CSV file

        Parameters
        ----------
        annotation : typing.Optional[int]

        annotators : typing.Optional[str]

        draft : typing.Optional[int]

        expand_created_by : typing.Optional[bool]

        projects : typing.Optional[str]

        tz : typing.Optional[str]
            Timezone in which to export the data. Format IANA timezone name, e.g. "America/New_York"

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Yields
        ------
        typing.Iterator[bytes]
            CSV file with comments
        """
        with self._client_wrapper.httpx_client.stream(
            "api/comments/export/",
            method="GET",
            params={
                "annotation": annotation,
                "annotators": annotators,
                "draft": draft,
                "expand_created_by": expand_created_by,
                "projects": projects,
                "tz": tz,
            },
            request_options=request_options,
        ) as _response:
            try:
                if 200 <= _response.status_code < 300:
                    _chunk_size = request_options.get("chunk_size", None) if request_options is not None else None
                    for _chunk in _response.iter_bytes(chunk_size=_chunk_size):
                        yield _chunk
                    return
                _response.read()
                _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,
        id: str,
        *,
        expand_created_by: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MaybeExpandedComment:
        """
        Retrieve a specific comment by ID for an annotation.

        Parameters
        ----------
        id : str

        expand_created_by : typing.Optional[bool]
            Expand the created_by field

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

        Returns
        -------
        MaybeExpandedComment


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.comments.get(
            id="id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/comments/{jsonable_encoder(id)}/",
            method="GET",
            params={
                "expand_created_by": expand_created_by,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    MaybeExpandedComment,
                    construct_type(
                        type_=MaybeExpandedComment,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 delete(
        self,
        id: str,
        *,
        expand_created_by: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Delete a comment by ID

        Parameters
        ----------
        id : str

        expand_created_by : typing.Optional[bool]
            Expand the created_by field

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.comments.delete(
            id="id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/comments/{jsonable_encoder(id)}/",
            method="DELETE",
            params={
                "expand_created_by": expand_created_by,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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 update(
        self,
        id: str,
        *,
        expand_created_by: typing.Optional[bool] = None,
        annotation: typing.Optional[int] = OMIT,
        classifications: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        draft: typing.Optional[int] = OMIT,
        is_resolved: typing.Optional[bool] = OMIT,
        region_ref: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        text: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MaybeExpandedComment:
        """
        Update a specific comment by ID.

        Parameters
        ----------
        id : str

        expand_created_by : typing.Optional[bool]
            Expand the created_by field

        annotation : typing.Optional[int]

        classifications : typing.Optional[typing.Optional[typing.Any]]

        draft : typing.Optional[int]

        is_resolved : typing.Optional[bool]
            True if the comment is resolved

        region_ref : typing.Optional[typing.Optional[typing.Any]]

        text : typing.Optional[str]
            Reviewer or annotator comment

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

        Returns
        -------
        MaybeExpandedComment


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.comments.update(
            id="id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/comments/{jsonable_encoder(id)}/",
            method="PATCH",
            params={
                "expand_created_by": expand_created_by,
            },
            json={
                "annotation": annotation,
                "classifications": classifications,
                "draft": draft,
                "is_resolved": is_resolved,
                "region_ref": region_ref,
                "text": text,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    MaybeExpandedComment,
                    construct_type(
                        type_=MaybeExpandedComment,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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)


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

    async def list(
        self,
        *,
        annotation: typing.Optional[int] = None,
        annotators: typing.Optional[str] = None,
        draft: typing.Optional[int] = None,
        expand_created_by: typing.Optional[bool] = None,
        ordering: typing.Optional[str] = None,
        projects: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[MaybeExpandedComment]:
        """
        List all comments for a specific annotation ID.

        Parameters
        ----------
        annotation : typing.Optional[int]

        annotators : typing.Optional[str]

        draft : typing.Optional[int]

        expand_created_by : typing.Optional[bool]

        ordering : typing.Optional[str]
            Which field to use when ordering the results.

        projects : typing.Optional[str]

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

        Returns
        -------
        typing.List[MaybeExpandedComment]
            List of comments

        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.comments.list()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/comments/",
            method="GET",
            params={
                "annotation": annotation,
                "annotators": annotators,
                "draft": draft,
                "expand_created_by": expand_created_by,
                "ordering": ordering,
                "projects": projects,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[MaybeExpandedComment],
                    construct_type(
                        type_=typing.List[MaybeExpandedComment],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 create(
        self,
        *,
        expand_created_by: typing.Optional[bool] = None,
        annotation: typing.Optional[int] = OMIT,
        classifications: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        draft: typing.Optional[int] = OMIT,
        is_resolved: typing.Optional[bool] = OMIT,
        region_ref: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        text: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MaybeExpandedComment:
        """
        Create a comment for a specific annotation ID.

        Parameters
        ----------
        expand_created_by : typing.Optional[bool]
            Expand the created_by field

        annotation : typing.Optional[int]

        classifications : typing.Optional[typing.Optional[typing.Any]]

        draft : typing.Optional[int]

        is_resolved : typing.Optional[bool]
            True if the comment is resolved

        region_ref : typing.Optional[typing.Optional[typing.Any]]

        text : typing.Optional[str]
            Reviewer or annotator comment

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

        Returns
        -------
        MaybeExpandedComment


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.comments.create()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/comments/",
            method="POST",
            params={
                "expand_created_by": expand_created_by,
            },
            json={
                "annotation": annotation,
                "classifications": classifications,
                "draft": draft,
                "is_resolved": is_resolved,
                "region_ref": region_ref,
                "text": text,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    MaybeExpandedComment,
                    construct_type(
                        type_=MaybeExpandedComment,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 export(
        self,
        *,
        annotation: typing.Optional[int] = None,
        annotators: typing.Optional[str] = None,
        draft: typing.Optional[int] = None,
        expand_created_by: typing.Optional[bool] = None,
        projects: typing.Optional[str] = None,
        tz: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.AsyncIterator[bytes]:
        """
        Export comments to CSV file

        Parameters
        ----------
        annotation : typing.Optional[int]

        annotators : typing.Optional[str]

        draft : typing.Optional[int]

        expand_created_by : typing.Optional[bool]

        projects : typing.Optional[str]

        tz : typing.Optional[str]
            Timezone in which to export the data. Format IANA timezone name, e.g. "America/New_York"

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Yields
        ------
        typing.AsyncIterator[bytes]
            CSV file with comments
        """
        async with self._client_wrapper.httpx_client.stream(
            "api/comments/export/",
            method="GET",
            params={
                "annotation": annotation,
                "annotators": annotators,
                "draft": draft,
                "expand_created_by": expand_created_by,
                "projects": projects,
                "tz": tz,
            },
            request_options=request_options,
        ) as _response:
            try:
                if 200 <= _response.status_code < 300:
                    _chunk_size = request_options.get("chunk_size", None) if request_options is not None else None
                    async for _chunk in _response.aiter_bytes(chunk_size=_chunk_size):
                        yield _chunk
                    return
                await _response.aread()
                _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,
        id: str,
        *,
        expand_created_by: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MaybeExpandedComment:
        """
        Retrieve a specific comment by ID for an annotation.

        Parameters
        ----------
        id : str

        expand_created_by : typing.Optional[bool]
            Expand the created_by field

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

        Returns
        -------
        MaybeExpandedComment


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.comments.get(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/comments/{jsonable_encoder(id)}/",
            method="GET",
            params={
                "expand_created_by": expand_created_by,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    MaybeExpandedComment,
                    construct_type(
                        type_=MaybeExpandedComment,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 delete(
        self,
        id: str,
        *,
        expand_created_by: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """
        Delete a comment by ID

        Parameters
        ----------
        id : str

        expand_created_by : typing.Optional[bool]
            Expand the created_by field

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.comments.delete(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/comments/{jsonable_encoder(id)}/",
            method="DELETE",
            params={
                "expand_created_by": expand_created_by,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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 update(
        self,
        id: str,
        *,
        expand_created_by: typing.Optional[bool] = None,
        annotation: typing.Optional[int] = OMIT,
        classifications: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        draft: typing.Optional[int] = OMIT,
        is_resolved: typing.Optional[bool] = OMIT,
        region_ref: typing.Optional[typing.Optional[typing.Any]] = OMIT,
        text: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MaybeExpandedComment:
        """
        Update a specific comment by ID.

        Parameters
        ----------
        id : str

        expand_created_by : typing.Optional[bool]
            Expand the created_by field

        annotation : typing.Optional[int]

        classifications : typing.Optional[typing.Optional[typing.Any]]

        draft : typing.Optional[int]

        is_resolved : typing.Optional[bool]
            True if the comment is resolved

        region_ref : typing.Optional[typing.Optional[typing.Any]]

        text : typing.Optional[str]
            Reviewer or annotator comment

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

        Returns
        -------
        MaybeExpandedComment


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.comments.update(
                id="id",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/comments/{jsonable_encoder(id)}/",
            method="PATCH",
            params={
                "expand_created_by": expand_created_by,
            },
            json={
                "annotation": annotation,
                "classifications": classifications,
                "draft": draft,
                "is_resolved": is_resolved,
                "region_ref": region_ref,
                "text": text,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    MaybeExpandedComment,
                    construct_type(
                        type_=MaybeExpandedComment,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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)
