# 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.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.internal_server_error import InternalServerError
from ...errors.payment_required_error import PaymentRequiredError
from ...errors.unauthorized_error import UnauthorizedError
from ...types.error import Error
from ...types.mms_content import MmsContent
from ...types.rcs_validate_content import RcsValidateContent
from ...types.sms_content import SmsContent
from .types.blast_mms_options import BlastMmsOptions
from .types.blast_mms_response import BlastMmsResponse
from .types.blast_rcs_options import BlastRcsOptions
from .types.blast_rcs_response import BlastRcsResponse
from .types.blast_sms_options import BlastSmsOptions
from .types.blast_sms_response import BlastSmsResponse

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


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

    def sms(
        self,
        *,
        audience_id: str,
        senders: typing.Sequence[str],
        message: SmsContent,
        options: typing.Optional[BlastSmsOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[BlastSmsResponse]:
        """
        Send an SMS message to all contacts in an audience. <br>

        Messages are distributed evenly across the provided sender phone numbers. <br>

        Use the optional `schedule` parameter in `options` to schedule the blast for future delivery. When scheduled, the response will contain a `scheduleId` instead of blast details.

        Parameters
        ----------
        audience_id : str
            The audience ID to send the blast to. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`. <br>

            You can create an audience via [the dashboard](https://app.pinnacle.sh/dashboard/audiences) or [API](/api-reference/audiences/create).

        senders : typing.Sequence[str]
            Array of phone numbers to send from in E.164 format. <br>

            Messages will be distributed evenly across these senders.

            > **Note:** Sandbox numbers cannot be used for blasts.

            **Limit:** 1 min

        message : SmsContent

        options : typing.Optional[BlastSmsOptions]
            Additional settings to customize SMS blast delivery.

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

        Returns
        -------
        HttpResponse[BlastSmsResponse]
            Successfully queued or scheduled the SMS blast. <br>

            - **Immediate blast**: Returns blast details including total recipients, messages, segments, and cost.
            - **Scheduled blast**: Returns a `scheduleId` and the schedule configuration.
        """
        _response = self._client_wrapper.httpx_client.request(
            "messages/blast/sms",
            method="POST",
            json={
                "audienceId": audience_id,
                "senders": senders,
                "message": convert_and_respect_annotation_metadata(
                    object_=message, annotation=SmsContent, direction="write"
                ),
                "options": convert_and_respect_annotation_metadata(
                    object_=options, annotation=BlastSmsOptions, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BlastSmsResponse,
                    parse_obj_as(
                        type_=BlastSmsResponse,  # 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(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 402:
                raise PaymentRequiredError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # 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 mms(
        self,
        *,
        audience_id: str,
        senders: typing.Sequence[str],
        message: MmsContent,
        options: typing.Optional[BlastMmsOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[BlastMmsResponse]:
        """
        Send an MMS message to all contacts in an audience. <br>

        Messages are distributed evenly across the provided sender phone numbers. <br>

        Use the optional `schedule` parameter in `options` to schedule the blast for future delivery. When scheduled, the response will contain a `scheduleId` instead of blast details.

        Parameters
        ----------
        audience_id : str
            The audience ID to send the blast to. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`. <br>

            You can create an audience via [the dashboard](https://app.pinnacle.sh/dashboard/audiences) or [API](/api-reference/audiences/create).

        senders : typing.Sequence[str]
            Array of phone numbers to send from in E.164 format. <br>

            Messages will be distributed evenly across these senders.

            > **Note:** Sandbox numbers cannot be used for blasts.

            **Limit:** 1 min

        message : MmsContent

        options : typing.Optional[BlastMmsOptions]
            Additional settings to customize MMS blast delivery.

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

        Returns
        -------
        HttpResponse[BlastMmsResponse]
            Successfully queued or scheduled the MMS blast. <br>

            - **Immediate blast**: Returns blast details including total recipients, messages, segments, and cost.
            - **Scheduled blast**: Returns a `scheduleId` and the schedule configuration.
        """
        _response = self._client_wrapper.httpx_client.request(
            "messages/blast/mms",
            method="POST",
            json={
                "audienceId": audience_id,
                "senders": senders,
                "message": convert_and_respect_annotation_metadata(
                    object_=message, annotation=MmsContent, direction="write"
                ),
                "options": convert_and_respect_annotation_metadata(
                    object_=options, annotation=BlastMmsOptions, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BlastMmsResponse,
                    parse_obj_as(
                        type_=BlastMmsResponse,  # 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(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 402:
                raise PaymentRequiredError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # 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 rcs(
        self,
        *,
        audience_id: str,
        senders: typing.Sequence[str],
        message: RcsValidateContent,
        options: typing.Optional[BlastRcsOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[BlastRcsResponse]:
        """
        Send an RCS message to all contacts in an audience. <br>

        Messages are distributed evenly across the provided RCS agents for load balancing. Requires active RCS agents and recipient devices that support RCS Business Messaging. <br>

        Use the optional `schedule` parameter in `options` to schedule the blast for future delivery. When scheduled, the response will contain a `scheduleId` instead of blast details.

        Parameters
        ----------
        audience_id : str
            The audience ID to send the blast to. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`. <br>

            You can create an audience via [the dashboard](https://app.pinnacle.sh/dashboard/audiences) or [API](/api-reference/audiences/create).

        senders : typing.Sequence[str]
            Array of RCS agent IDs to send from. Each must be prefixed with `agent_`. <br>

            Messages will be evenly distributed across these agents.

            > **Note:** Test agents cannot be used for blasts.

            **Limit:** 1 min

        message : RcsValidateContent

        options : typing.Optional[BlastRcsOptions]
            Configure how your RCS blast is sent and tracked.

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

        Returns
        -------
        HttpResponse[BlastRcsResponse]
            Successfully queued or scheduled the RCS blast. <br>

            - **Immediate blast**: Returns blast details including total recipients, messages, segments, and cost.
            - **Scheduled blast**: Returns a `scheduleId` and the schedule configuration.
        """
        _response = self._client_wrapper.httpx_client.request(
            "messages/blast/rcs",
            method="POST",
            json={
                "audienceId": audience_id,
                "senders": senders,
                "message": convert_and_respect_annotation_metadata(
                    object_=message, annotation=RcsValidateContent, direction="write"
                ),
                "options": convert_and_respect_annotation_metadata(
                    object_=options, annotation=BlastRcsOptions, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BlastRcsResponse,
                    parse_obj_as(
                        type_=BlastRcsResponse,  # 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(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 402:
                raise PaymentRequiredError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # 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 AsyncRawBlastClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def sms(
        self,
        *,
        audience_id: str,
        senders: typing.Sequence[str],
        message: SmsContent,
        options: typing.Optional[BlastSmsOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[BlastSmsResponse]:
        """
        Send an SMS message to all contacts in an audience. <br>

        Messages are distributed evenly across the provided sender phone numbers. <br>

        Use the optional `schedule` parameter in `options` to schedule the blast for future delivery. When scheduled, the response will contain a `scheduleId` instead of blast details.

        Parameters
        ----------
        audience_id : str
            The audience ID to send the blast to. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`. <br>

            You can create an audience via [the dashboard](https://app.pinnacle.sh/dashboard/audiences) or [API](/api-reference/audiences/create).

        senders : typing.Sequence[str]
            Array of phone numbers to send from in E.164 format. <br>

            Messages will be distributed evenly across these senders.

            > **Note:** Sandbox numbers cannot be used for blasts.

            **Limit:** 1 min

        message : SmsContent

        options : typing.Optional[BlastSmsOptions]
            Additional settings to customize SMS blast delivery.

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

        Returns
        -------
        AsyncHttpResponse[BlastSmsResponse]
            Successfully queued or scheduled the SMS blast. <br>

            - **Immediate blast**: Returns blast details including total recipients, messages, segments, and cost.
            - **Scheduled blast**: Returns a `scheduleId` and the schedule configuration.
        """
        _response = await self._client_wrapper.httpx_client.request(
            "messages/blast/sms",
            method="POST",
            json={
                "audienceId": audience_id,
                "senders": senders,
                "message": convert_and_respect_annotation_metadata(
                    object_=message, annotation=SmsContent, direction="write"
                ),
                "options": convert_and_respect_annotation_metadata(
                    object_=options, annotation=BlastSmsOptions, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BlastSmsResponse,
                    parse_obj_as(
                        type_=BlastSmsResponse,  # 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(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 402:
                raise PaymentRequiredError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # 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 mms(
        self,
        *,
        audience_id: str,
        senders: typing.Sequence[str],
        message: MmsContent,
        options: typing.Optional[BlastMmsOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[BlastMmsResponse]:
        """
        Send an MMS message to all contacts in an audience. <br>

        Messages are distributed evenly across the provided sender phone numbers. <br>

        Use the optional `schedule` parameter in `options` to schedule the blast for future delivery. When scheduled, the response will contain a `scheduleId` instead of blast details.

        Parameters
        ----------
        audience_id : str
            The audience ID to send the blast to. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`. <br>

            You can create an audience via [the dashboard](https://app.pinnacle.sh/dashboard/audiences) or [API](/api-reference/audiences/create).

        senders : typing.Sequence[str]
            Array of phone numbers to send from in E.164 format. <br>

            Messages will be distributed evenly across these senders.

            > **Note:** Sandbox numbers cannot be used for blasts.

            **Limit:** 1 min

        message : MmsContent

        options : typing.Optional[BlastMmsOptions]
            Additional settings to customize MMS blast delivery.

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

        Returns
        -------
        AsyncHttpResponse[BlastMmsResponse]
            Successfully queued or scheduled the MMS blast. <br>

            - **Immediate blast**: Returns blast details including total recipients, messages, segments, and cost.
            - **Scheduled blast**: Returns a `scheduleId` and the schedule configuration.
        """
        _response = await self._client_wrapper.httpx_client.request(
            "messages/blast/mms",
            method="POST",
            json={
                "audienceId": audience_id,
                "senders": senders,
                "message": convert_and_respect_annotation_metadata(
                    object_=message, annotation=MmsContent, direction="write"
                ),
                "options": convert_and_respect_annotation_metadata(
                    object_=options, annotation=BlastMmsOptions, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BlastMmsResponse,
                    parse_obj_as(
                        type_=BlastMmsResponse,  # 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(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 402:
                raise PaymentRequiredError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # 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 rcs(
        self,
        *,
        audience_id: str,
        senders: typing.Sequence[str],
        message: RcsValidateContent,
        options: typing.Optional[BlastRcsOptions] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[BlastRcsResponse]:
        """
        Send an RCS message to all contacts in an audience. <br>

        Messages are distributed evenly across the provided RCS agents for load balancing. Requires active RCS agents and recipient devices that support RCS Business Messaging. <br>

        Use the optional `schedule` parameter in `options` to schedule the blast for future delivery. When scheduled, the response will contain a `scheduleId` instead of blast details.

        Parameters
        ----------
        audience_id : str
            The audience ID to send the blast to. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`. <br>

            You can create an audience via [the dashboard](https://app.pinnacle.sh/dashboard/audiences) or [API](/api-reference/audiences/create).

        senders : typing.Sequence[str]
            Array of RCS agent IDs to send from. Each must be prefixed with `agent_`. <br>

            Messages will be evenly distributed across these agents.

            > **Note:** Test agents cannot be used for blasts.

            **Limit:** 1 min

        message : RcsValidateContent

        options : typing.Optional[BlastRcsOptions]
            Configure how your RCS blast is sent and tracked.

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

        Returns
        -------
        AsyncHttpResponse[BlastRcsResponse]
            Successfully queued or scheduled the RCS blast. <br>

            - **Immediate blast**: Returns blast details including total recipients, messages, segments, and cost.
            - **Scheduled blast**: Returns a `scheduleId` and the schedule configuration.
        """
        _response = await self._client_wrapper.httpx_client.request(
            "messages/blast/rcs",
            method="POST",
            json={
                "audienceId": audience_id,
                "senders": senders,
                "message": convert_and_respect_annotation_metadata(
                    object_=message, annotation=RcsValidateContent, direction="write"
                ),
                "options": convert_and_respect_annotation_metadata(
                    object_=options, annotation=BlastRcsOptions, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BlastRcsResponse,
                    parse_obj_as(
                        type_=BlastRcsResponse,  # 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(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 402:
                raise PaymentRequiredError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # 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)
