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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError as core_api_error_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 ..core.serialization import convert_and_respect_annotation_metadata
from ..errors.bad_request_error import BadRequestError
from ..errors.forbidden_error import ForbiddenError
from ..errors.internal_server_error import InternalServerError
from ..errors.not_found_error import NotFoundError
from ..errors.unauthorized_error import UnauthorizedError
from ..types.api_error import ApiError as types_api_error_ApiError
from ..types.custom_plan_config import CustomPlanConfig
from ..types.ordered_plans_in_group import OrderedPlansInGroup
from .types.create_plan_group_response import CreatePlanGroupResponse
from .types.get_plan_group_response import GetPlanGroupResponse
from .types.update_plan_group_response import UpdatePlanGroupResponse

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


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

    def get_plan_group(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetPlanGroupResponse]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[GetPlanGroupResponse]
            OK
        """
        _response = self._client_wrapper.httpx_client.request(
            "plan-groups",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetPlanGroupResponse,
                    parse_obj_as(
                        type_=GetPlanGroupResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise core_api_error_ApiError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
            )
        raise core_api_error_ApiError(
            status_code=_response.status_code, headers=dict(_response.headers), body=_response_json
        )

    def create_plan_group(
        self,
        *,
        add_on_ids: typing.Sequence[str],
        ordered_plans: typing.Sequence[OrderedPlansInGroup],
        custom_plan_config: typing.Optional[CustomPlanConfig] = OMIT,
        custom_plan_id: typing.Optional[str] = OMIT,
        default_plan_id: typing.Optional[str] = OMIT,
        trial_days: typing.Optional[int] = OMIT,
        trial_payment_method_required: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreatePlanGroupResponse]:
        """
        Parameters
        ----------
        add_on_ids : typing.Sequence[str]

        ordered_plans : typing.Sequence[OrderedPlansInGroup]

        custom_plan_config : typing.Optional[CustomPlanConfig]

        custom_plan_id : typing.Optional[str]

        default_plan_id : typing.Optional[str]

        trial_days : typing.Optional[int]

        trial_payment_method_required : typing.Optional[bool]

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

        Returns
        -------
        HttpResponse[CreatePlanGroupResponse]
            Created
        """
        _response = self._client_wrapper.httpx_client.request(
            "plan-groups",
            method="POST",
            json={
                "add_on_ids": add_on_ids,
                "custom_plan_config": convert_and_respect_annotation_metadata(
                    object_=custom_plan_config, annotation=CustomPlanConfig, direction="write"
                ),
                "custom_plan_id": custom_plan_id,
                "default_plan_id": default_plan_id,
                "ordered_plans": convert_and_respect_annotation_metadata(
                    object_=ordered_plans, annotation=typing.Sequence[OrderedPlansInGroup], direction="write"
                ),
                "trial_days": trial_days,
                "trial_payment_method_required": trial_payment_method_required,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreatePlanGroupResponse,
                    parse_obj_as(
                        type_=CreatePlanGroupResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise core_api_error_ApiError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
            )
        raise core_api_error_ApiError(
            status_code=_response.status_code, headers=dict(_response.headers), body=_response_json
        )

    def update_plan_group(
        self,
        plan_group_id: str,
        *,
        add_on_ids: typing.Sequence[str],
        ordered_plans: typing.Sequence[OrderedPlansInGroup],
        custom_plan_config: typing.Optional[CustomPlanConfig] = OMIT,
        custom_plan_id: typing.Optional[str] = OMIT,
        default_plan_id: typing.Optional[str] = OMIT,
        trial_days: typing.Optional[int] = OMIT,
        trial_payment_method_required: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[UpdatePlanGroupResponse]:
        """
        Parameters
        ----------
        plan_group_id : str
            plan_group_id

        add_on_ids : typing.Sequence[str]

        ordered_plans : typing.Sequence[OrderedPlansInGroup]

        custom_plan_config : typing.Optional[CustomPlanConfig]

        custom_plan_id : typing.Optional[str]

        default_plan_id : typing.Optional[str]

        trial_days : typing.Optional[int]

        trial_payment_method_required : typing.Optional[bool]

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

        Returns
        -------
        HttpResponse[UpdatePlanGroupResponse]
            OK
        """
        _response = self._client_wrapper.httpx_client.request(
            f"plan-groups/{jsonable_encoder(plan_group_id)}",
            method="PUT",
            json={
                "add_on_ids": add_on_ids,
                "custom_plan_config": convert_and_respect_annotation_metadata(
                    object_=custom_plan_config, annotation=CustomPlanConfig, direction="write"
                ),
                "custom_plan_id": custom_plan_id,
                "default_plan_id": default_plan_id,
                "ordered_plans": convert_and_respect_annotation_metadata(
                    object_=ordered_plans, annotation=typing.Sequence[OrderedPlansInGroup], direction="write"
                ),
                "trial_days": trial_days,
                "trial_payment_method_required": trial_payment_method_required,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdatePlanGroupResponse,
                    parse_obj_as(
                        type_=UpdatePlanGroupResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise core_api_error_ApiError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
            )
        raise core_api_error_ApiError(
            status_code=_response.status_code, headers=dict(_response.headers), body=_response_json
        )


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

    async def get_plan_group(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetPlanGroupResponse]:
        """
        Parameters
        ----------
        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[GetPlanGroupResponse]
            OK
        """
        _response = await self._client_wrapper.httpx_client.request(
            "plan-groups",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetPlanGroupResponse,
                    parse_obj_as(
                        type_=GetPlanGroupResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise core_api_error_ApiError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
            )
        raise core_api_error_ApiError(
            status_code=_response.status_code, headers=dict(_response.headers), body=_response_json
        )

    async def create_plan_group(
        self,
        *,
        add_on_ids: typing.Sequence[str],
        ordered_plans: typing.Sequence[OrderedPlansInGroup],
        custom_plan_config: typing.Optional[CustomPlanConfig] = OMIT,
        custom_plan_id: typing.Optional[str] = OMIT,
        default_plan_id: typing.Optional[str] = OMIT,
        trial_days: typing.Optional[int] = OMIT,
        trial_payment_method_required: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreatePlanGroupResponse]:
        """
        Parameters
        ----------
        add_on_ids : typing.Sequence[str]

        ordered_plans : typing.Sequence[OrderedPlansInGroup]

        custom_plan_config : typing.Optional[CustomPlanConfig]

        custom_plan_id : typing.Optional[str]

        default_plan_id : typing.Optional[str]

        trial_days : typing.Optional[int]

        trial_payment_method_required : typing.Optional[bool]

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

        Returns
        -------
        AsyncHttpResponse[CreatePlanGroupResponse]
            Created
        """
        _response = await self._client_wrapper.httpx_client.request(
            "plan-groups",
            method="POST",
            json={
                "add_on_ids": add_on_ids,
                "custom_plan_config": convert_and_respect_annotation_metadata(
                    object_=custom_plan_config, annotation=CustomPlanConfig, direction="write"
                ),
                "custom_plan_id": custom_plan_id,
                "default_plan_id": default_plan_id,
                "ordered_plans": convert_and_respect_annotation_metadata(
                    object_=ordered_plans, annotation=typing.Sequence[OrderedPlansInGroup], direction="write"
                ),
                "trial_days": trial_days,
                "trial_payment_method_required": trial_payment_method_required,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreatePlanGroupResponse,
                    parse_obj_as(
                        type_=CreatePlanGroupResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise core_api_error_ApiError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
            )
        raise core_api_error_ApiError(
            status_code=_response.status_code, headers=dict(_response.headers), body=_response_json
        )

    async def update_plan_group(
        self,
        plan_group_id: str,
        *,
        add_on_ids: typing.Sequence[str],
        ordered_plans: typing.Sequence[OrderedPlansInGroup],
        custom_plan_config: typing.Optional[CustomPlanConfig] = OMIT,
        custom_plan_id: typing.Optional[str] = OMIT,
        default_plan_id: typing.Optional[str] = OMIT,
        trial_days: typing.Optional[int] = OMIT,
        trial_payment_method_required: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[UpdatePlanGroupResponse]:
        """
        Parameters
        ----------
        plan_group_id : str
            plan_group_id

        add_on_ids : typing.Sequence[str]

        ordered_plans : typing.Sequence[OrderedPlansInGroup]

        custom_plan_config : typing.Optional[CustomPlanConfig]

        custom_plan_id : typing.Optional[str]

        default_plan_id : typing.Optional[str]

        trial_days : typing.Optional[int]

        trial_payment_method_required : typing.Optional[bool]

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

        Returns
        -------
        AsyncHttpResponse[UpdatePlanGroupResponse]
            OK
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"plan-groups/{jsonable_encoder(plan_group_id)}",
            method="PUT",
            json={
                "add_on_ids": add_on_ids,
                "custom_plan_config": convert_and_respect_annotation_metadata(
                    object_=custom_plan_config, annotation=CustomPlanConfig, direction="write"
                ),
                "custom_plan_id": custom_plan_id,
                "default_plan_id": default_plan_id,
                "ordered_plans": convert_and_respect_annotation_metadata(
                    object_=ordered_plans, annotation=typing.Sequence[OrderedPlansInGroup], direction="write"
                ),
                "trial_days": trial_days,
                "trial_payment_method_required": trial_payment_method_required,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    UpdatePlanGroupResponse,
                    parse_obj_as(
                        type_=UpdatePlanGroupResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        types_api_error_ApiError,
                        parse_obj_as(
                            type_=types_api_error_ApiError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise core_api_error_ApiError(
                status_code=_response.status_code, headers=dict(_response.headers), body=_response.text
            )
        raise core_api_error_ApiError(
            status_code=_response.status_code, headers=dict(_response.headers), body=_response_json
        )
