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

import typing

from ... import core
from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.request_options import RequestOptions
from .raw_client import AsyncRawAudienceClient, RawAudienceClient
from .types.delete_audience_id_members_response import DeleteAudienceIdMembersResponse
from .types.delete_audience_id_response import DeleteAudienceIdResponse
from .types.get_audience_id_members_response import GetAudienceIdMembersResponse
from .types.get_audience_id_members_search_response import GetAudienceIdMembersSearchResponse
from .types.get_audience_id_response import GetAudienceIdResponse
from .types.get_audience_response import GetAudienceResponse
from .types.post_audience_id_members_response import PostAudienceIdMembersResponse
from .types.post_audience_response import PostAudienceResponse

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


class AudienceClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawAudienceClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawAudienceClient
        """
        return self._raw_client

    def get_all_audiences(self, *, request_options: typing.Optional[RequestOptions] = None) -> GetAudienceResponse:
        """
        Retrieve all audiences created by the authenticated user. Users can only access audiences they have created.

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

        Returns
        -------
        GetAudienceResponse
            Successfully retrieved audiences

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.get_all_audiences()
        """
        _response = self._raw_client.get_all_audiences(request_options=request_options)
        return _response.data

    def create_audience_with_csv_upload(
        self,
        *,
        name: str,
        phone_number_column_name: str,
        identifier_column_name: str,
        file: core.File,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostAudienceResponse:
        """
        Create a new audience by uploading a CSV file containing phone numbers

        Parameters
        ----------
        name : str
            The name of the audience

        phone_number_column_name : str
            The name of the column in the CSV that contains phone numbers

        identifier_column_name : str
            The name of the column in the CSV that contains identifiers (e.g., names)

        file : core.File
            See core.File for more documentation

        description : typing.Optional[str]
            Optional description of the audience

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

        Returns
        -------
        PostAudienceResponse
            Audience created successfully

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.create_audience_with_csv_upload(
            name="name",
            phone_number_column_name="phoneNumberColumnName",
            identifier_column_name="identifierColumnName",
        )
        """
        _response = self._raw_client.create_audience_with_csv_upload(
            name=name,
            phone_number_column_name=phone_number_column_name,
            identifier_column_name=identifier_column_name,
            file=file,
            description=description,
            request_options=request_options,
        )
        return _response.data

    def get_audience_by_id(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetAudienceIdResponse:
        """
        Retrieve a specific audience by its ID. Users can only access audiences they have created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

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

        Returns
        -------
        GetAudienceIdResponse
            Successfully retrieved audience

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.get_audience_by_id(
            id="60d0fe4f5311236168a109ca",
        )
        """
        _response = self._raw_client.get_audience_by_id(id, request_options=request_options)
        return _response.data

    def delete_audience(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteAudienceIdResponse:
        """
        Delete a specific audience by its ID. Users can only delete audiences they created. Cannot delete if audience is being used by a campaign.

        Parameters
        ----------
        id : str
            The unique identifier of the audience to delete

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

        Returns
        -------
        DeleteAudienceIdResponse
            Audience deleted successfully

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.delete_audience(
            id="60d0fe4f5311236168a109ca",
        )
        """
        _response = self._raw_client.delete_audience(id, request_options=request_options)
        return _response.data

    def get_audience_members(
        self,
        id: str,
        *,
        page: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetAudienceIdMembersResponse:
        """
        Retrieve members of a specific audience with pagination support. Users can only access members of audiences they created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        page : typing.Optional[int]
            Page number for pagination (default is 1)

        limit : typing.Optional[int]
            Number of items per page (default is 5)

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

        Returns
        -------
        GetAudienceIdMembersResponse
            Successfully retrieved audience members

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.get_audience_members(
            id="60d0fe4f5311236168a109ca",
            page=1,
            limit=10,
        )
        """
        _response = self._raw_client.get_audience_members(id, page=page, limit=limit, request_options=request_options)
        return _response.data

    def add_audience_members(
        self,
        id: str,
        *,
        members: typing.Sequence[typing.Dict[str, typing.Any]],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostAudienceIdMembersResponse:
        """
        Add new members to an existing audience. Users can only add members to audiences they created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        members : typing.Sequence[typing.Dict[str, typing.Any]]
            Array of member objects with dynamic structure based on audience configuration

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

        Returns
        -------
        PostAudienceIdMembersResponse
            Members added successfully

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.add_audience_members(
            id="60d0fe4f5311236168a109ca",
            members=[
                {
                    "phoneNumber": "+1234567890",
                    "name": "John Doe",
                    "email": "john@example.com",
                }
            ],
        )
        """
        _response = self._raw_client.add_audience_members(id, members=members, request_options=request_options)
        return _response.data

    def delete_audience_members(
        self, id: str, *, member_ids: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteAudienceIdMembersResponse:
        """
        Remove specific members from an audience by their member IDs. Users can only delete members from audiences they created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        member_ids : typing.Sequence[str]
            Array of member IDs to delete

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

        Returns
        -------
        DeleteAudienceIdMembersResponse
            Members deleted successfully

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.delete_audience_members(
            id="60d0fe4f5311236168a109ca",
            member_ids=["60d0fe4f5311236168a109cd"],
        )
        """
        _response = self._raw_client.delete_audience_members(id, member_ids=member_ids, request_options=request_options)
        return _response.data

    def search_audience_members(
        self, id: str, *, query: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> GetAudienceIdMembersSearchResponse:
        """
        Search for members within a specific audience using flexible search parameters. Users can only search members of audiences they created.

        **Search Types:**
        - **General Search** (`query`): Searches across all fields in the audience member data
        - **Field-Specific Search**: Use any field name as a parameter (e.g., `firstName=john`, `phoneNumber=123456`, `email=test@example.com`)

        **Examples:**
        - `?query=john` - General search across all fields
        - `?firstName=john` - Search specifically in firstName field
        - `?phoneNumber=555-1234` - Search specifically in phoneNumber field
        - `?firstName=john&lastName=doe` - Search for members matching both criteria

        **Note:** When using phoneNumber field, do not use quotes around the phone number. You can use either a general search OR field-specific searches, but not both simultaneously.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        query : typing.Optional[str]
            General search term that searches across all fields in audience member data

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

        Returns
        -------
        GetAudienceIdMembersSearchResponse
            Search results returned successfully

        Examples
        --------
        from smallestai import SmallestAI

        client = SmallestAI(
            token="YOUR_TOKEN",
        )
        client.atoms.audience.search_audience_members(
            id="60d0fe4f5311236168a109ca",
            query="john",
        )
        """
        _response = self._raw_client.search_audience_members(id, query=query, request_options=request_options)
        return _response.data


class AsyncAudienceClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawAudienceClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawAudienceClient
        """
        return self._raw_client

    async def get_all_audiences(
        self, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetAudienceResponse:
        """
        Retrieve all audiences created by the authenticated user. Users can only access audiences they have created.

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

        Returns
        -------
        GetAudienceResponse
            Successfully retrieved audiences

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.get_all_audiences()


        asyncio.run(main())
        """
        _response = await self._raw_client.get_all_audiences(request_options=request_options)
        return _response.data

    async def create_audience_with_csv_upload(
        self,
        *,
        name: str,
        phone_number_column_name: str,
        identifier_column_name: str,
        file: core.File,
        description: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostAudienceResponse:
        """
        Create a new audience by uploading a CSV file containing phone numbers

        Parameters
        ----------
        name : str
            The name of the audience

        phone_number_column_name : str
            The name of the column in the CSV that contains phone numbers

        identifier_column_name : str
            The name of the column in the CSV that contains identifiers (e.g., names)

        file : core.File
            See core.File for more documentation

        description : typing.Optional[str]
            Optional description of the audience

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

        Returns
        -------
        PostAudienceResponse
            Audience created successfully

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.create_audience_with_csv_upload(
                name="name",
                phone_number_column_name="phoneNumberColumnName",
                identifier_column_name="identifierColumnName",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_audience_with_csv_upload(
            name=name,
            phone_number_column_name=phone_number_column_name,
            identifier_column_name=identifier_column_name,
            file=file,
            description=description,
            request_options=request_options,
        )
        return _response.data

    async def get_audience_by_id(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> GetAudienceIdResponse:
        """
        Retrieve a specific audience by its ID. Users can only access audiences they have created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

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

        Returns
        -------
        GetAudienceIdResponse
            Successfully retrieved audience

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.get_audience_by_id(
                id="60d0fe4f5311236168a109ca",
            )


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

    async def delete_audience(
        self, id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteAudienceIdResponse:
        """
        Delete a specific audience by its ID. Users can only delete audiences they created. Cannot delete if audience is being used by a campaign.

        Parameters
        ----------
        id : str
            The unique identifier of the audience to delete

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

        Returns
        -------
        DeleteAudienceIdResponse
            Audience deleted successfully

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.delete_audience(
                id="60d0fe4f5311236168a109ca",
            )


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

    async def get_audience_members(
        self,
        id: str,
        *,
        page: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> GetAudienceIdMembersResponse:
        """
        Retrieve members of a specific audience with pagination support. Users can only access members of audiences they created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        page : typing.Optional[int]
            Page number for pagination (default is 1)

        limit : typing.Optional[int]
            Number of items per page (default is 5)

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

        Returns
        -------
        GetAudienceIdMembersResponse
            Successfully retrieved audience members

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.get_audience_members(
                id="60d0fe4f5311236168a109ca",
                page=1,
                limit=10,
            )


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

    async def add_audience_members(
        self,
        id: str,
        *,
        members: typing.Sequence[typing.Dict[str, typing.Any]],
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PostAudienceIdMembersResponse:
        """
        Add new members to an existing audience. Users can only add members to audiences they created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        members : typing.Sequence[typing.Dict[str, typing.Any]]
            Array of member objects with dynamic structure based on audience configuration

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

        Returns
        -------
        PostAudienceIdMembersResponse
            Members added successfully

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.add_audience_members(
                id="60d0fe4f5311236168a109ca",
                members=[
                    {
                        "phoneNumber": "+1234567890",
                        "name": "John Doe",
                        "email": "john@example.com",
                    }
                ],
            )


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

    async def delete_audience_members(
        self, id: str, *, member_ids: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> DeleteAudienceIdMembersResponse:
        """
        Remove specific members from an audience by their member IDs. Users can only delete members from audiences they created.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        member_ids : typing.Sequence[str]
            Array of member IDs to delete

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

        Returns
        -------
        DeleteAudienceIdMembersResponse
            Members deleted successfully

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.delete_audience_members(
                id="60d0fe4f5311236168a109ca",
                member_ids=["60d0fe4f5311236168a109cd"],
            )


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

    async def search_audience_members(
        self, id: str, *, query: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> GetAudienceIdMembersSearchResponse:
        """
        Search for members within a specific audience using flexible search parameters. Users can only search members of audiences they created.

        **Search Types:**
        - **General Search** (`query`): Searches across all fields in the audience member data
        - **Field-Specific Search**: Use any field name as a parameter (e.g., `firstName=john`, `phoneNumber=123456`, `email=test@example.com`)

        **Examples:**
        - `?query=john` - General search across all fields
        - `?firstName=john` - Search specifically in firstName field
        - `?phoneNumber=555-1234` - Search specifically in phoneNumber field
        - `?firstName=john&lastName=doe` - Search for members matching both criteria

        **Note:** When using phoneNumber field, do not use quotes around the phone number. You can use either a general search OR field-specific searches, but not both simultaneously.

        Parameters
        ----------
        id : str
            The unique identifier of the audience

        query : typing.Optional[str]
            General search term that searches across all fields in audience member data

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

        Returns
        -------
        GetAudienceIdMembersSearchResponse
            Search results returned successfully

        Examples
        --------
        import asyncio

        from smallestai import AsyncSmallestAI

        client = AsyncSmallestAI(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.atoms.audience.search_audience_members(
                id="60d0fe4f5311236168a109ca",
                query="john",
            )


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