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

from __future__ import annotations

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.audience_count_only import AudienceCountOnly
from ..types.delete_audience_response import DeleteAudienceResponse
from .raw_client import AsyncRawAudiencesClient, RawAudiencesClient
from .types.audiences_get_response import AudiencesGetResponse

if typing.TYPE_CHECKING:
    from .contacts.client import AsyncContactsClient, ContactsClient
# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class AudiencesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawAudiencesClient(client_wrapper=client_wrapper)
        self._client_wrapper = client_wrapper
        self._contacts: typing.Optional[ContactsClient] = None

    @property
    def with_raw_response(self) -> RawAudiencesClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawAudiencesClient
        """
        return self._raw_client

    def get(
        self,
        *,
        id: str,
        page: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AudiencesGetResponse:
        """
        Retrieve an audience by ID with optional pagination.

        Parameters
        ----------
        id : str
            Audience ID. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`.

        page : typing.Optional[int]
            Page number. If provided with or without limit, returns paginated contacts.

        limit : typing.Optional[int]
            Items per page. If provided with or without page, returns paginated contacts.

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

        Returns
        -------
        AudiencesGetResponse
            Successfully retrieved audience.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.audiences.get(
            id="aud_abc123",
        )
        """
        _response = self._raw_client.get(id=id, page=page, limit=limit, request_options=request_options)
        return _response.data

    def create(
        self,
        *,
        name: str,
        description: typing.Optional[str] = OMIT,
        contacts: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AudienceCountOnly:
        """
        Create a new audience with optional initial contacts. Phone numbers that don't exist will be auto-created as contacts.

        Parameters
        ----------
        name : str
            Audience name.

        description : typing.Optional[str]
            Audience description.

        contacts : typing.Optional[typing.Sequence[str]]
            Optional array of phone numbers (E.164 format) or contact IDs.

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

        Returns
        -------
        AudienceCountOnly
            Successfully created the audience.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.audiences.create(
            name="Marketing Campaign Q1",
            description="Contacts for Q1 marketing push",
        )
        """
        _response = self._raw_client.create(
            name=name, description=description, contacts=contacts, request_options=request_options
        )
        return _response.data

    def delete(self, *, id: str, request_options: typing.Optional[RequestOptions] = None) -> DeleteAudienceResponse:
        """
        Permanently delete an audience and all its contact associations.

        Note: This will NOT delete the contacts themselves, only the audience and its memberships.

        Parameters
        ----------
        id : str
            Audience ID. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`.

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

        Returns
        -------
        DeleteAudienceResponse
            Successfully deleted the audience.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.audiences.delete(
            id="aud_abc123",
        )
        """
        _response = self._raw_client.delete(id=id, request_options=request_options)
        return _response.data

    def update(
        self,
        *,
        id: str,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AudienceCountOnly:
        """
        Update audience metadata. This endpoint does NOT modify contacts.

        To add or remove contacts, use the [Add Contacts](/api-reference/audiences/add-contacts) or [Remove Contacts](/api-reference/audiences/remove-contacts) endpoints.

        Parameters
        ----------
        id : str
            Audience ID to update. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`.

        name : typing.Optional[str]
            New audience name.

        description : typing.Optional[str]
            New audience description.

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

        Returns
        -------
        AudienceCountOnly
            Successfully updated the audience.

        Examples
        --------
        from rcs import Pinnacle

        client = Pinnacle(
            api_key="YOUR_API_KEY",
        )
        client.audiences.update(
            id="aud_abc123",
            name="Updated Audience Name",
            description="New description",
        )
        """
        _response = self._raw_client.update(id=id, name=name, description=description, request_options=request_options)
        return _response.data

    @property
    def contacts(self):
        if self._contacts is None:
            from .contacts.client import ContactsClient  # noqa: E402

            self._contacts = ContactsClient(client_wrapper=self._client_wrapper)
        return self._contacts


class AsyncAudiencesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawAudiencesClient(client_wrapper=client_wrapper)
        self._client_wrapper = client_wrapper
        self._contacts: typing.Optional[AsyncContactsClient] = None

    @property
    def with_raw_response(self) -> AsyncRawAudiencesClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawAudiencesClient
        """
        return self._raw_client

    async def get(
        self,
        *,
        id: str,
        page: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AudiencesGetResponse:
        """
        Retrieve an audience by ID with optional pagination.

        Parameters
        ----------
        id : str
            Audience ID. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`.

        page : typing.Optional[int]
            Page number. If provided with or without limit, returns paginated contacts.

        limit : typing.Optional[int]
            Items per page. If provided with or without page, returns paginated contacts.

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

        Returns
        -------
        AudiencesGetResponse
            Successfully retrieved audience.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


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


        asyncio.run(main())
        """
        _response = await self._raw_client.get(id=id, page=page, limit=limit, request_options=request_options)
        return _response.data

    async def create(
        self,
        *,
        name: str,
        description: typing.Optional[str] = OMIT,
        contacts: typing.Optional[typing.Sequence[str]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AudienceCountOnly:
        """
        Create a new audience with optional initial contacts. Phone numbers that don't exist will be auto-created as contacts.

        Parameters
        ----------
        name : str
            Audience name.

        description : typing.Optional[str]
            Audience description.

        contacts : typing.Optional[typing.Sequence[str]]
            Optional array of phone numbers (E.164 format) or contact IDs.

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

        Returns
        -------
        AudienceCountOnly
            Successfully created the audience.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.audiences.create(
                name="Marketing Campaign Q1",
                description="Contacts for Q1 marketing push",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(
            name=name, description=description, contacts=contacts, request_options=request_options
        )
        return _response.data

    async def delete(
        self, *, id: str, request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteAudienceResponse:
        """
        Permanently delete an audience and all its contact associations.

        Note: This will NOT delete the contacts themselves, only the audience and its memberships.

        Parameters
        ----------
        id : str
            Audience ID. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`.

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

        Returns
        -------
        DeleteAudienceResponse
            Successfully deleted the audience.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


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


        asyncio.run(main())
        """
        _response = await self._raw_client.delete(id=id, request_options=request_options)
        return _response.data

    async def update(
        self,
        *,
        id: str,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AudienceCountOnly:
        """
        Update audience metadata. This endpoint does NOT modify contacts.

        To add or remove contacts, use the [Add Contacts](/api-reference/audiences/add-contacts) or [Remove Contacts](/api-reference/audiences/remove-contacts) endpoints.

        Parameters
        ----------
        id : str
            Audience ID to update. This identifier is a string that always begins with the prefix `aud_`, for example: `aud_abc123`.

        name : typing.Optional[str]
            New audience name.

        description : typing.Optional[str]
            New audience description.

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

        Returns
        -------
        AudienceCountOnly
            Successfully updated the audience.

        Examples
        --------
        import asyncio

        from rcs import AsyncPinnacle

        client = AsyncPinnacle(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.audiences.update(
                id="aud_abc123",
                name="Updated Audience Name",
                description="New description",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update(
            id=id, name=name, description=description, request_options=request_options
        )
        return _response.data

    @property
    def contacts(self):
        if self._contacts is None:
            from .contacts.client import AsyncContactsClient  # noqa: E402

            self._contacts = AsyncContactsClient(client_wrapper=self._client_wrapper)
        return self._contacts
