# 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.create_flowplate_key_response import CreateFlowplateKeyResponse
from ..types.flowplate_key_action_response import FlowplateKeyActionResponse
from ..types.refresh_flowplate_key_response import RefreshFlowplateKeyResponse

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


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

    def create_flowplate_api_key(
        self,
        *,
        flowplate_master_key: str,
        user_id: str,
        organization_id: str,
        metadata: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateFlowplateKeyResponse]:
        """
        Create a hidden API key for a Flowplate user+org.

        Called by Flowplate backend during user login.
        Requires X-Flowplate-Master-Key header.

        The raw API key is returned ONCE and must be stored securely by Flowplate.

        Parameters
        ----------
        flowplate_master_key : str
            Flowplate master API key

        user_id : str
            Aitronos user ID

        organization_id : str
            Organization ID

        metadata : typing.Optional[typing.Dict[str, typing.Any]]
            Optional metadata for the key

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

        Returns
        -------
        HttpResponse[CreateFlowplateKeyResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/flowplate/api-keys",
            method="POST",
            json={
                "user_id": user_id,
                "organization_id": organization_id,
                "metadata": metadata,
            },
            headers={
                "content-type": "application/json",
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateFlowplateKeyResponse,
                    parse_obj_as(
                        type_=CreateFlowplateKeyResponse,  # 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 refresh_flowplate_api_key(
        self,
        user_id: str,
        org_id: str,
        *,
        flowplate_master_key: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[RefreshFlowplateKeyResponse]:
        """
        Rotate a user's Flowplate API key.

        Deletes the old key and creates a new one with the same settings.
        Requires X-Flowplate-Master-Key header.

        The new raw API key is returned ONCE and must be stored securely by Flowplate.

        Parameters
        ----------
        user_id : str

        org_id : str

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        HttpResponse[RefreshFlowplateKeyResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/flowplate/api-keys/{jsonable_encoder(user_id)}/{jsonable_encoder(org_id)}/refresh",
            method="POST",
            headers={
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RefreshFlowplateKeyResponse,
                    parse_obj_as(
                        type_=RefreshFlowplateKeyResponse,  # 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 revoke_flowplate_api_key(
        self,
        user_id: str,
        org_id: str,
        *,
        flowplate_master_key: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[FlowplateKeyActionResponse]:
        """
        Revoke a user's Flowplate API key.

        Soft-deletes the key so it can no longer be used.
        Requires X-Flowplate-Master-Key header.

        Parameters
        ----------
        user_id : str

        org_id : str

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        HttpResponse[FlowplateKeyActionResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/flowplate/api-keys/{jsonable_encoder(user_id)}/{jsonable_encoder(org_id)}",
            method="DELETE",
            headers={
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FlowplateKeyActionResponse,
                    parse_obj_as(
                        type_=FlowplateKeyActionResponse,  # 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 revoke_all_user_keys(
        self, user_id: str, *, flowplate_master_key: str, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[FlowplateKeyActionResponse]:
        """
        Revoke all Flowplate API keys for a user.

        Called when a user account is deactivated.
        Requires X-Flowplate-Master-Key header.

        Parameters
        ----------
        user_id : str

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        HttpResponse[FlowplateKeyActionResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/flowplate/api-keys/user/{jsonable_encoder(user_id)}",
            method="DELETE",
            headers={
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FlowplateKeyActionResponse,
                    parse_obj_as(
                        type_=FlowplateKeyActionResponse,  # 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 AsyncRawFlowplateIntegrationClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create_flowplate_api_key(
        self,
        *,
        flowplate_master_key: str,
        user_id: str,
        organization_id: str,
        metadata: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateFlowplateKeyResponse]:
        """
        Create a hidden API key for a Flowplate user+org.

        Called by Flowplate backend during user login.
        Requires X-Flowplate-Master-Key header.

        The raw API key is returned ONCE and must be stored securely by Flowplate.

        Parameters
        ----------
        flowplate_master_key : str
            Flowplate master API key

        user_id : str
            Aitronos user ID

        organization_id : str
            Organization ID

        metadata : typing.Optional[typing.Dict[str, typing.Any]]
            Optional metadata for the key

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

        Returns
        -------
        AsyncHttpResponse[CreateFlowplateKeyResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/flowplate/api-keys",
            method="POST",
            json={
                "user_id": user_id,
                "organization_id": organization_id,
                "metadata": metadata,
            },
            headers={
                "content-type": "application/json",
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateFlowplateKeyResponse,
                    parse_obj_as(
                        type_=CreateFlowplateKeyResponse,  # 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 refresh_flowplate_api_key(
        self,
        user_id: str,
        org_id: str,
        *,
        flowplate_master_key: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[RefreshFlowplateKeyResponse]:
        """
        Rotate a user's Flowplate API key.

        Deletes the old key and creates a new one with the same settings.
        Requires X-Flowplate-Master-Key header.

        The new raw API key is returned ONCE and must be stored securely by Flowplate.

        Parameters
        ----------
        user_id : str

        org_id : str

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        AsyncHttpResponse[RefreshFlowplateKeyResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/flowplate/api-keys/{jsonable_encoder(user_id)}/{jsonable_encoder(org_id)}/refresh",
            method="POST",
            headers={
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RefreshFlowplateKeyResponse,
                    parse_obj_as(
                        type_=RefreshFlowplateKeyResponse,  # 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 revoke_flowplate_api_key(
        self,
        user_id: str,
        org_id: str,
        *,
        flowplate_master_key: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[FlowplateKeyActionResponse]:
        """
        Revoke a user's Flowplate API key.

        Soft-deletes the key so it can no longer be used.
        Requires X-Flowplate-Master-Key header.

        Parameters
        ----------
        user_id : str

        org_id : str

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        AsyncHttpResponse[FlowplateKeyActionResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/flowplate/api-keys/{jsonable_encoder(user_id)}/{jsonable_encoder(org_id)}",
            method="DELETE",
            headers={
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FlowplateKeyActionResponse,
                    parse_obj_as(
                        type_=FlowplateKeyActionResponse,  # 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 revoke_all_user_keys(
        self, user_id: str, *, flowplate_master_key: str, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[FlowplateKeyActionResponse]:
        """
        Revoke all Flowplate API keys for a user.

        Called when a user account is deactivated.
        Requires X-Flowplate-Master-Key header.

        Parameters
        ----------
        user_id : str

        flowplate_master_key : str
            Flowplate master API key

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

        Returns
        -------
        AsyncHttpResponse[FlowplateKeyActionResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/flowplate/api-keys/user/{jsonable_encoder(user_id)}",
            method="DELETE",
            headers={
                "x-flowplate-master-key": str(flowplate_master_key) if flowplate_master_key is not None else None,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FlowplateKeyActionResponse,
                    parse_obj_as(
                        type_=FlowplateKeyActionResponse,  # 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)
