# 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.icon_list_response import IconListResponse
from ..types.icon_response import IconResponse

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


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

    def list_icons(
        self,
        org_id: str,
        *,
        search: typing.Optional[str] = None,
        category: typing.Optional[str] = None,
        include_inactive: typing.Optional[bool] = None,
        recommended_for: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[IconListResponse]:
        """
        List icons available to an organization.

        Returns system icons + organization's custom icons.
        Supports search, category filtering, and pagination.

        Use `recommended_for=assistants` to show icons best suited for assistants first.
        Future contexts: projects, teams, etc.

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

        search : typing.Optional[str]
            Search by name, description, or tags

        category : typing.Optional[str]
            Filter by category

        include_inactive : typing.Optional[bool]
            Include inactive icons

        recommended_for : typing.Optional[str]
            Show icons recommended for context (e.g., 'assistants') first

        limit : typing.Optional[int]
            Items per page

        offset : typing.Optional[int]
            Number of items to skip

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

        Returns
        -------
        HttpResponse[IconListResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}",
            method="GET",
            params={
                "search": search,
                "category": category,
                "include_inactive": include_inactive,
                "recommended_for": recommended_for,
                "limit": limit,
                "offset": offset,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    IconListResponse,
                    parse_obj_as(
                        type_=IconListResponse,  # 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 upload_icon(
        self, org_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[IconResponse]:
        """
        Upload a custom icon for an organization.

        Accepts **multipart/form-data** (fields: `file`, `name`, optional `description`,
        `tags`, `category`) or **application/json** with a base64-encoded image:
        ```json
        {"image": "<raw-base64>", "name": "optional-name"}
        ```
        File must be SVG, PNG, or JPG, max 5 MB.

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

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

        Returns
        -------
        HttpResponse[IconResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    IconResponse,
                    parse_obj_as(
                        type_=IconResponse,  # 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 delete_org_logo(
        self, org_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[None]:
        """
        Remove the logo from an organization.

        Clears logo_image and image_url on the organization.
        If the logo references a custom icon owned by this org, that icon is also soft-deleted.
        No-op (204) if the org has no logo set.

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

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            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 serve_icon(
        self,
        icon_id: str,
        *,
        quality: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Any]:
        """
        Serve icon file with optional quality transformation.

        Icons are publicly accessible since they're stored in a public GCS bucket.
        This endpoint acts as a proxy to GCS with quality transformation support.
        Quality parameter controls image size for raster formats.
        SVG icons always return original regardless of quality.

        Parameters
        ----------
        icon_id : str

        quality : typing.Optional[str]
            Quality: thumbnail, small, medium, large, original

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

        Returns
        -------
        HttpResponse[typing.Any]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/icons/{jsonable_encoder(icon_id)}",
            method="GET",
            params={
                "quality": quality,
            },
            request_options=request_options,
        )
        try:
            if _response is None or not _response.text.strip():
                return HttpResponse(response=_response, data=None)
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Any,
                    parse_obj_as(
                        type_=typing.Any,  # 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 delete_icon(
        self, org_id: str, icon_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[None]:
        """
        Soft delete a custom icon.

        Marks icon as inactive and marks file for cleanup.
        Cannot delete system icons.

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

        icon_id : str

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

        Returns
        -------
        HttpResponse[None]
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}/{jsonable_encoder(icon_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return HttpResponse(response=_response, data=None)
            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 update_icon_metadata(
        self,
        org_id: str,
        icon_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[IconResponse]:
        """
        Update icon metadata (custom icons only).

        Can update name, description, tags, and is_active status.
        Cannot update system icons.

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

        icon_id : str

        name : typing.Optional[str]

        description : typing.Optional[str]

        tags : typing.Optional[typing.Sequence[str]]

        is_active : typing.Optional[bool]

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

        Returns
        -------
        HttpResponse[IconResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}/{jsonable_encoder(icon_id)}",
            method="PATCH",
            json={
                "name": name,
                "description": description,
                "tags": tags,
                "is_active": is_active,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    IconResponse,
                    parse_obj_as(
                        type_=IconResponse,  # 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 AsyncRawIconsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list_icons(
        self,
        org_id: str,
        *,
        search: typing.Optional[str] = None,
        category: typing.Optional[str] = None,
        include_inactive: typing.Optional[bool] = None,
        recommended_for: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[IconListResponse]:
        """
        List icons available to an organization.

        Returns system icons + organization's custom icons.
        Supports search, category filtering, and pagination.

        Use `recommended_for=assistants` to show icons best suited for assistants first.
        Future contexts: projects, teams, etc.

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

        search : typing.Optional[str]
            Search by name, description, or tags

        category : typing.Optional[str]
            Filter by category

        include_inactive : typing.Optional[bool]
            Include inactive icons

        recommended_for : typing.Optional[str]
            Show icons recommended for context (e.g., 'assistants') first

        limit : typing.Optional[int]
            Items per page

        offset : typing.Optional[int]
            Number of items to skip

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

        Returns
        -------
        AsyncHttpResponse[IconListResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}",
            method="GET",
            params={
                "search": search,
                "category": category,
                "include_inactive": include_inactive,
                "recommended_for": recommended_for,
                "limit": limit,
                "offset": offset,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    IconListResponse,
                    parse_obj_as(
                        type_=IconListResponse,  # 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 upload_icon(
        self, org_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[IconResponse]:
        """
        Upload a custom icon for an organization.

        Accepts **multipart/form-data** (fields: `file`, `name`, optional `description`,
        `tags`, `category`) or **application/json** with a base64-encoded image:
        ```json
        {"image": "<raw-base64>", "name": "optional-name"}
        ```
        File must be SVG, PNG, or JPG, max 5 MB.

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

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

        Returns
        -------
        AsyncHttpResponse[IconResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}",
            method="POST",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    IconResponse,
                    parse_obj_as(
                        type_=IconResponse,  # 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 delete_org_logo(
        self, org_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[None]:
        """
        Remove the logo from an organization.

        Clears logo_image and image_url on the organization.
        If the logo references a custom icon owned by this org, that icon is also soft-deleted.
        No-op (204) if the org has no logo set.

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

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            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 serve_icon(
        self,
        icon_id: str,
        *,
        quality: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Any]:
        """
        Serve icon file with optional quality transformation.

        Icons are publicly accessible since they're stored in a public GCS bucket.
        This endpoint acts as a proxy to GCS with quality transformation support.
        Quality parameter controls image size for raster formats.
        SVG icons always return original regardless of quality.

        Parameters
        ----------
        icon_id : str

        quality : typing.Optional[str]
            Quality: thumbnail, small, medium, large, original

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

        Returns
        -------
        AsyncHttpResponse[typing.Any]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/icons/{jsonable_encoder(icon_id)}",
            method="GET",
            params={
                "quality": quality,
            },
            request_options=request_options,
        )
        try:
            if _response is None or not _response.text.strip():
                return AsyncHttpResponse(response=_response, data=None)
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Any,
                    parse_obj_as(
                        type_=typing.Any,  # 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 delete_icon(
        self, org_id: str, icon_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[None]:
        """
        Soft delete a custom icon.

        Marks icon as inactive and marks file for cleanup.
        Cannot delete system icons.

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

        icon_id : str

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

        Returns
        -------
        AsyncHttpResponse[None]
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}/{jsonable_encoder(icon_id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return AsyncHttpResponse(response=_response, data=None)
            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 update_icon_metadata(
        self,
        org_id: str,
        icon_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        tags: typing.Optional[typing.Sequence[str]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[IconResponse]:
        """
        Update icon metadata (custom icons only).

        Can update name, description, tags, and is_active status.
        Cannot update system icons.

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

        icon_id : str

        name : typing.Optional[str]

        description : typing.Optional[str]

        tags : typing.Optional[typing.Sequence[str]]

        is_active : typing.Optional[bool]

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

        Returns
        -------
        AsyncHttpResponse[IconResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/icons/organizations/{jsonable_encoder(org_id)}/{jsonable_encoder(icon_id)}",
            method="PATCH",
            json={
                "name": name,
                "description": description,
                "tags": tags,
                "is_active": is_active,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    IconResponse,
                    parse_obj_as(
                        type_=IconResponse,  # 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)
