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

import typing

from ....core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ....core.request_options import RequestOptions
from ....types.conversation_initiation_source import ConversationInitiationSource
from ....types.conversation_product import ConversationProduct
from ....types.evaluation_success_result import EvaluationSuccessResult
from ....types.message_search_sort_by import MessageSearchSortBy
from ....types.messages_search_response import MessagesSearchResponse
from .raw_client import AsyncRawMessagesClient, RawMessagesClient
from .types.messages_text_search_request_exclude_statuses_item import MessagesTextSearchRequestExcludeStatusesItem
from .types.messages_text_search_request_summary_mode import MessagesTextSearchRequestSummaryMode


class MessagesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawMessagesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawMessagesClient
        """
        return self._raw_client

    def text_search(
        self,
        *,
        text_query: str,
        agent_id: typing.Optional[str] = None,
        visited_agent_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        visited_agent_branch_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        call_successful: typing.Optional[EvaluationSuccessResult] = None,
        call_start_before_unix: typing.Optional[int] = None,
        call_start_after_unix: typing.Optional[int] = None,
        call_duration_min_secs: typing.Optional[int] = None,
        call_duration_max_secs: typing.Optional[int] = None,
        rating_max: typing.Optional[int] = None,
        rating_min: typing.Optional[int] = None,
        has_feedback_comment: typing.Optional[bool] = None,
        user_id: typing.Optional[str] = None,
        evaluation_params: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        data_collection_params: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        tool_names: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        tool_names_successful: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        tool_names_errored: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        main_languages: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        exclude_statuses: typing.Optional[
            typing.Union[
                MessagesTextSearchRequestExcludeStatusesItem,
                typing.Sequence[MessagesTextSearchRequestExcludeStatusesItem],
            ]
        ] = None,
        termination_reasons: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        page_size: typing.Optional[int] = None,
        summary_mode: typing.Optional[MessagesTextSearchRequestSummaryMode] = None,
        conversation_initiation_source: typing.Optional[ConversationInitiationSource] = None,
        text_only: typing.Optional[bool] = None,
        conversation_product_type: typing.Optional[ConversationProduct] = None,
        branch_id: typing.Optional[str] = None,
        version_id: typing.Optional[str] = None,
        topic_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        sort_by: typing.Optional[MessageSearchSortBy] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MessagesSearchResponse:
        """
        Search through conversation transcript messages by full-text and fuzzy search

        Parameters
        ----------
        text_query : str
            The search query text for full-text and fuzzy matching

        agent_id : typing.Optional[str]
            Agent id (agent_…) or speech engine external id (seng_), resolved to the same underlying resource.

        visited_agent_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations where any of these agents participated. Can not exceed 50 values.

        visited_agent_branch_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations where any of these agent branches participated. Can not exceed 50 values.

        call_successful : typing.Optional[EvaluationSuccessResult]
            The result of the success evaluation

        call_start_before_unix : typing.Optional[int]
            Unix timestamp (in seconds) to filter conversations up to this start date.

        call_start_after_unix : typing.Optional[int]
            Unix timestamp (in seconds) to filter conversations after to this start date.

        call_duration_min_secs : typing.Optional[int]
            Minimum call duration in seconds.

        call_duration_max_secs : typing.Optional[int]
            Maximum call duration in seconds.

        rating_max : typing.Optional[int]
            Maximum overall rating (1-5).

        rating_min : typing.Optional[int]
            Minimum overall rating (1-5).

        has_feedback_comment : typing.Optional[bool]
            Filter conversations with user feedback comments.

        user_id : typing.Optional[str]
            Filter conversations by the user ID who initiated them.

        evaluation_params : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Evaluation filters. Repeat param. Format: criteria_id:result. Example: eval=value_framing:success

        data_collection_params : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Data collection filters. Repeat param. Format: id:op:value where op is one of eq|neq|gt|gte|lt|lte|in|exists|missing. For in, pipe-delimit values.

        tool_names : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by tool names used during the call.

        tool_names_successful : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by tool names that had successful calls.

        tool_names_errored : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by tool names that had errored calls.

        main_languages : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by detected main language (language code).

        exclude_statuses : typing.Optional[typing.Union[MessagesTextSearchRequestExcludeStatusesItem, typing.Sequence[MessagesTextSearchRequestExcludeStatusesItem]]]
            Exclude conversations with the given statuses. Useful for hiding in-progress / processing conversations from list views.

        termination_reasons : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by their stored termination_reason (metadata.termination_reason). Repeat param to match any of several.

        page_size : typing.Optional[int]
            Number of results per page. Max 50.

        summary_mode : typing.Optional[MessagesTextSearchRequestSummaryMode]
            Whether to include transcript summaries in the response.

        conversation_initiation_source : typing.Optional[ConversationInitiationSource]

        text_only : typing.Optional[bool]

        conversation_product_type : typing.Optional[ConversationProduct]
            Restrict results to a single conversation product surface.

        branch_id : typing.Optional[str]
            Filter conversations by branch ID.

        version_id : typing.Optional[str]
            Filter conversations by version ID.

        topic_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by topic IDs assigned during topic discovery.

        sort_by : typing.Optional[MessageSearchSortBy]
            Sort order for search results. 'search_score' sorts by search score, 'created_at' sorts by conversation start time.

        cursor : typing.Optional[str]
            Used for fetching next page. Cursor is returned in the response.

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

        Returns
        -------
        MessagesSearchResponse
            Successful Response

        Examples
        --------
        from elevenlabs import ElevenLabs

        client = ElevenLabs(
            api_key="YOUR_API_KEY",
        )
        client.conversational_ai.conversations.messages.text_search(
            text_query="refund policy",
            agent_id="agent_id",
            visited_agent_ids=["visited_agent_ids"],
            visited_agent_branch_ids=["visited_agent_branch_ids"],
            call_successful="success",
            call_start_before_unix=1,
            call_start_after_unix=1,
            call_duration_min_secs=1,
            call_duration_max_secs=1,
            rating_max=1,
            rating_min=1,
            has_feedback_comment=True,
            user_id="user_id",
            evaluation_params=["evaluation_params"],
            data_collection_params=["data_collection_params"],
            tool_names=["tool_names"],
            tool_names_successful=["tool_names_successful"],
            tool_names_errored=["tool_names_errored"],
            main_languages=["main_languages"],
            exclude_statuses=["initiated"],
            termination_reasons=["termination_reasons"],
            page_size=1,
            summary_mode="exclude",
            conversation_initiation_source="unknown",
            text_only=True,
            conversation_product_type="agents",
            branch_id="branch_id",
            version_id="version_id",
            topic_ids=["topic_ids"],
            sort_by="search_score",
            cursor="cursor",
        )
        """
        _response = self._raw_client.text_search(
            text_query=text_query,
            agent_id=agent_id,
            visited_agent_ids=visited_agent_ids,
            visited_agent_branch_ids=visited_agent_branch_ids,
            call_successful=call_successful,
            call_start_before_unix=call_start_before_unix,
            call_start_after_unix=call_start_after_unix,
            call_duration_min_secs=call_duration_min_secs,
            call_duration_max_secs=call_duration_max_secs,
            rating_max=rating_max,
            rating_min=rating_min,
            has_feedback_comment=has_feedback_comment,
            user_id=user_id,
            evaluation_params=evaluation_params,
            data_collection_params=data_collection_params,
            tool_names=tool_names,
            tool_names_successful=tool_names_successful,
            tool_names_errored=tool_names_errored,
            main_languages=main_languages,
            exclude_statuses=exclude_statuses,
            termination_reasons=termination_reasons,
            page_size=page_size,
            summary_mode=summary_mode,
            conversation_initiation_source=conversation_initiation_source,
            text_only=text_only,
            conversation_product_type=conversation_product_type,
            branch_id=branch_id,
            version_id=version_id,
            topic_ids=topic_ids,
            sort_by=sort_by,
            cursor=cursor,
            request_options=request_options,
        )
        return _response.data

    def search(
        self,
        *,
        text_query: str,
        agent_id: typing.Optional[str] = None,
        page_size: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MessagesSearchResponse:
        """
        Search conversation transcripts by semantic similarity to surface relevant messages based on meaning and intent, rather than exact keyword matches

        Parameters
        ----------
        text_query : str
            The search query text for semantic similarity matching

        agent_id : typing.Optional[str]
            Agent id (agent_…) or speech engine external id (seng_), resolved to the same underlying resource.

        page_size : typing.Optional[int]
            Number of results per page. Max 50.

        cursor : typing.Optional[str]
            Used for fetching next page. Cursor is returned in the response.

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

        Returns
        -------
        MessagesSearchResponse
            Successful Response

        Examples
        --------
        from elevenlabs import ElevenLabs

        client = ElevenLabs(
            api_key="YOUR_API_KEY",
        )
        client.conversational_ai.conversations.messages.search(
            text_query="Customer asking to cancel and get money back",
            agent_id="agent_id",
            page_size=1,
            cursor="cursor",
        )
        """
        _response = self._raw_client.search(
            text_query=text_query,
            agent_id=agent_id,
            page_size=page_size,
            cursor=cursor,
            request_options=request_options,
        )
        return _response.data


class AsyncMessagesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawMessagesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawMessagesClient
        """
        return self._raw_client

    async def text_search(
        self,
        *,
        text_query: str,
        agent_id: typing.Optional[str] = None,
        visited_agent_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        visited_agent_branch_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        call_successful: typing.Optional[EvaluationSuccessResult] = None,
        call_start_before_unix: typing.Optional[int] = None,
        call_start_after_unix: typing.Optional[int] = None,
        call_duration_min_secs: typing.Optional[int] = None,
        call_duration_max_secs: typing.Optional[int] = None,
        rating_max: typing.Optional[int] = None,
        rating_min: typing.Optional[int] = None,
        has_feedback_comment: typing.Optional[bool] = None,
        user_id: typing.Optional[str] = None,
        evaluation_params: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        data_collection_params: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        tool_names: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        tool_names_successful: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        tool_names_errored: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        main_languages: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        exclude_statuses: typing.Optional[
            typing.Union[
                MessagesTextSearchRequestExcludeStatusesItem,
                typing.Sequence[MessagesTextSearchRequestExcludeStatusesItem],
            ]
        ] = None,
        termination_reasons: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        page_size: typing.Optional[int] = None,
        summary_mode: typing.Optional[MessagesTextSearchRequestSummaryMode] = None,
        conversation_initiation_source: typing.Optional[ConversationInitiationSource] = None,
        text_only: typing.Optional[bool] = None,
        conversation_product_type: typing.Optional[ConversationProduct] = None,
        branch_id: typing.Optional[str] = None,
        version_id: typing.Optional[str] = None,
        topic_ids: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        sort_by: typing.Optional[MessageSearchSortBy] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MessagesSearchResponse:
        """
        Search through conversation transcript messages by full-text and fuzzy search

        Parameters
        ----------
        text_query : str
            The search query text for full-text and fuzzy matching

        agent_id : typing.Optional[str]
            Agent id (agent_…) or speech engine external id (seng_), resolved to the same underlying resource.

        visited_agent_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations where any of these agents participated. Can not exceed 50 values.

        visited_agent_branch_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations where any of these agent branches participated. Can not exceed 50 values.

        call_successful : typing.Optional[EvaluationSuccessResult]
            The result of the success evaluation

        call_start_before_unix : typing.Optional[int]
            Unix timestamp (in seconds) to filter conversations up to this start date.

        call_start_after_unix : typing.Optional[int]
            Unix timestamp (in seconds) to filter conversations after to this start date.

        call_duration_min_secs : typing.Optional[int]
            Minimum call duration in seconds.

        call_duration_max_secs : typing.Optional[int]
            Maximum call duration in seconds.

        rating_max : typing.Optional[int]
            Maximum overall rating (1-5).

        rating_min : typing.Optional[int]
            Minimum overall rating (1-5).

        has_feedback_comment : typing.Optional[bool]
            Filter conversations with user feedback comments.

        user_id : typing.Optional[str]
            Filter conversations by the user ID who initiated them.

        evaluation_params : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Evaluation filters. Repeat param. Format: criteria_id:result. Example: eval=value_framing:success

        data_collection_params : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Data collection filters. Repeat param. Format: id:op:value where op is one of eq|neq|gt|gte|lt|lte|in|exists|missing. For in, pipe-delimit values.

        tool_names : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by tool names used during the call.

        tool_names_successful : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by tool names that had successful calls.

        tool_names_errored : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by tool names that had errored calls.

        main_languages : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by detected main language (language code).

        exclude_statuses : typing.Optional[typing.Union[MessagesTextSearchRequestExcludeStatusesItem, typing.Sequence[MessagesTextSearchRequestExcludeStatusesItem]]]
            Exclude conversations with the given statuses. Useful for hiding in-progress / processing conversations from list views.

        termination_reasons : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by their stored termination_reason (metadata.termination_reason). Repeat param to match any of several.

        page_size : typing.Optional[int]
            Number of results per page. Max 50.

        summary_mode : typing.Optional[MessagesTextSearchRequestSummaryMode]
            Whether to include transcript summaries in the response.

        conversation_initiation_source : typing.Optional[ConversationInitiationSource]

        text_only : typing.Optional[bool]

        conversation_product_type : typing.Optional[ConversationProduct]
            Restrict results to a single conversation product surface.

        branch_id : typing.Optional[str]
            Filter conversations by branch ID.

        version_id : typing.Optional[str]
            Filter conversations by version ID.

        topic_ids : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Filter conversations by topic IDs assigned during topic discovery.

        sort_by : typing.Optional[MessageSearchSortBy]
            Sort order for search results. 'search_score' sorts by search score, 'created_at' sorts by conversation start time.

        cursor : typing.Optional[str]
            Used for fetching next page. Cursor is returned in the response.

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

        Returns
        -------
        MessagesSearchResponse
            Successful Response

        Examples
        --------
        import asyncio

        from elevenlabs import AsyncElevenLabs

        client = AsyncElevenLabs(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.conversational_ai.conversations.messages.text_search(
                text_query="refund policy",
                agent_id="agent_id",
                visited_agent_ids=["visited_agent_ids"],
                visited_agent_branch_ids=["visited_agent_branch_ids"],
                call_successful="success",
                call_start_before_unix=1,
                call_start_after_unix=1,
                call_duration_min_secs=1,
                call_duration_max_secs=1,
                rating_max=1,
                rating_min=1,
                has_feedback_comment=True,
                user_id="user_id",
                evaluation_params=["evaluation_params"],
                data_collection_params=["data_collection_params"],
                tool_names=["tool_names"],
                tool_names_successful=["tool_names_successful"],
                tool_names_errored=["tool_names_errored"],
                main_languages=["main_languages"],
                exclude_statuses=["initiated"],
                termination_reasons=["termination_reasons"],
                page_size=1,
                summary_mode="exclude",
                conversation_initiation_source="unknown",
                text_only=True,
                conversation_product_type="agents",
                branch_id="branch_id",
                version_id="version_id",
                topic_ids=["topic_ids"],
                sort_by="search_score",
                cursor="cursor",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.text_search(
            text_query=text_query,
            agent_id=agent_id,
            visited_agent_ids=visited_agent_ids,
            visited_agent_branch_ids=visited_agent_branch_ids,
            call_successful=call_successful,
            call_start_before_unix=call_start_before_unix,
            call_start_after_unix=call_start_after_unix,
            call_duration_min_secs=call_duration_min_secs,
            call_duration_max_secs=call_duration_max_secs,
            rating_max=rating_max,
            rating_min=rating_min,
            has_feedback_comment=has_feedback_comment,
            user_id=user_id,
            evaluation_params=evaluation_params,
            data_collection_params=data_collection_params,
            tool_names=tool_names,
            tool_names_successful=tool_names_successful,
            tool_names_errored=tool_names_errored,
            main_languages=main_languages,
            exclude_statuses=exclude_statuses,
            termination_reasons=termination_reasons,
            page_size=page_size,
            summary_mode=summary_mode,
            conversation_initiation_source=conversation_initiation_source,
            text_only=text_only,
            conversation_product_type=conversation_product_type,
            branch_id=branch_id,
            version_id=version_id,
            topic_ids=topic_ids,
            sort_by=sort_by,
            cursor=cursor,
            request_options=request_options,
        )
        return _response.data

    async def search(
        self,
        *,
        text_query: str,
        agent_id: typing.Optional[str] = None,
        page_size: typing.Optional[int] = None,
        cursor: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> MessagesSearchResponse:
        """
        Search conversation transcripts by semantic similarity to surface relevant messages based on meaning and intent, rather than exact keyword matches

        Parameters
        ----------
        text_query : str
            The search query text for semantic similarity matching

        agent_id : typing.Optional[str]
            Agent id (agent_…) or speech engine external id (seng_), resolved to the same underlying resource.

        page_size : typing.Optional[int]
            Number of results per page. Max 50.

        cursor : typing.Optional[str]
            Used for fetching next page. Cursor is returned in the response.

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

        Returns
        -------
        MessagesSearchResponse
            Successful Response

        Examples
        --------
        import asyncio

        from elevenlabs import AsyncElevenLabs

        client = AsyncElevenLabs(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.conversational_ai.conversations.messages.search(
                text_query="Customer asking to cancel and get money back",
                agent_id="agent_id",
                page_size=1,
                cursor="cursor",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.search(
            text_query=text_query,
            agent_id=agent_id,
            page_size=page_size,
            cursor=cursor,
            request_options=request_options,
        )
        return _response.data
