"""
Auto-generated messages resource.

DO NOT EDIT THIS FILE MANUALLY.
Run `python scripts/generate_resources.py` to regenerate.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
    from ..client.base import BaseClient


class MessagesResource:
    """
    messages operations.
    """

    def __init__(self, client: BaseClient) -> None:
        self._client = client

    def _build_params(self, **kwargs: Any) -> dict[str, Any]:
        """Build query parameters, filtering None values."""

        def to_camel(s: str) -> str:
            parts = s.split("_")
            return parts[0] + "".join(p.title() for p in parts[1:])

        return {to_camel(k): v for k, v in kwargs.items() if v is not None}

    def _build_payload(self, **kwargs: Any) -> dict[str, Any]:
        """Build request payload, filtering None values."""
        from datetime import datetime

        def to_camel(s: str) -> str:
            parts = s.split("_")
            return parts[0] + "".join(p.title() for p in parts[1:])

        result: dict[str, Any] = {}
        for k, v in kwargs.items():
            if v is None:
                continue
            if isinstance(v, datetime):
                result[to_camel(k)] = v.isoformat()
            else:
                result[to_camel(k)] = v
        return result

    def list_inbox_conversations(
        self,
        *,
        profile_id: str | None = None,
        platform: str | None = None,
        status: str | None = None,
        sort_order: str | None = "desc",
        limit: int | None = 50,
        cursor: str | None = None,
        account_id: str | None = None,
    ) -> dict[str, Any]:
        """List conversations across all accounts"""
        params = self._build_params(
            profile_id=profile_id,
            platform=platform,
            status=status,
            sort_order=sort_order,
            limit=limit,
            cursor=cursor,
            account_id=account_id,
        )
        return self._client._get("/v1/inbox/conversations", params=params)

    def get_inbox_conversation(
        self, conversation_id: str, account_id: str
    ) -> dict[str, Any]:
        """Get conversation details"""
        params = self._build_params(
            account_id=account_id,
        )
        return self._client._get(
            f"/v1/inbox/conversations/{conversation_id}", params=params
        )

    def update_inbox_conversation(
        self, conversation_id: str, account_id: str, status: str
    ) -> dict[str, Any]:
        """Update conversation status"""
        payload = self._build_payload(
            account_id=account_id,
            status=status,
        )
        return self._client._put(
            f"/v1/inbox/conversations/{conversation_id}", data=payload
        )

    def get_inbox_conversation_messages(
        self, conversation_id: str, account_id: str
    ) -> dict[str, Any]:
        """Get messages in a conversation"""
        params = self._build_params(
            account_id=account_id,
        )
        return self._client._get(
            f"/v1/inbox/conversations/{conversation_id}/messages", params=params
        )

    def send_inbox_message(
        self,
        conversation_id: str,
        account_id: str,
        *,
        message: str | None = None,
        quick_replies: list[dict[str, Any]] | None = None,
        buttons: list[dict[str, Any]] | None = None,
        template: dict[str, Any] | None = None,
        reply_markup: dict[str, Any] | None = None,
        messaging_type: str | None = None,
        message_tag: str | None = None,
        reply_to: str | None = None,
    ) -> dict[str, Any]:
        """Send a message"""
        payload = self._build_payload(
            account_id=account_id,
            message=message,
            quick_replies=quick_replies,
            buttons=buttons,
            template=template,
            reply_markup=reply_markup,
            messaging_type=messaging_type,
            message_tag=message_tag,
            reply_to=reply_to,
        )
        return self._client._post(
            f"/v1/inbox/conversations/{conversation_id}/messages", data=payload
        )

    def edit_inbox_message(
        self,
        conversation_id: str,
        message_id: str,
        account_id: str,
        *,
        text: str | None = None,
        reply_markup: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Edit a message (Telegram only)"""
        payload = self._build_payload(
            account_id=account_id,
            text=text,
            reply_markup=reply_markup,
        )
        return self._client._patch(
            f"/v1/inbox/conversations/{conversation_id}/messages/{message_id}",
            data=payload,
        )

    async def alist_inbox_conversations(
        self,
        *,
        profile_id: str | None = None,
        platform: str | None = None,
        status: str | None = None,
        sort_order: str | None = "desc",
        limit: int | None = 50,
        cursor: str | None = None,
        account_id: str | None = None,
    ) -> dict[str, Any]:
        """List conversations across all accounts (async)"""
        params = self._build_params(
            profile_id=profile_id,
            platform=platform,
            status=status,
            sort_order=sort_order,
            limit=limit,
            cursor=cursor,
            account_id=account_id,
        )
        return await self._client._aget("/v1/inbox/conversations", params=params)

    async def aget_inbox_conversation(
        self, conversation_id: str, account_id: str
    ) -> dict[str, Any]:
        """Get conversation details (async)"""
        params = self._build_params(
            account_id=account_id,
        )
        return await self._client._aget(
            f"/v1/inbox/conversations/{conversation_id}", params=params
        )

    async def aupdate_inbox_conversation(
        self, conversation_id: str, account_id: str, status: str
    ) -> dict[str, Any]:
        """Update conversation status (async)"""
        payload = self._build_payload(
            account_id=account_id,
            status=status,
        )
        return await self._client._aput(
            f"/v1/inbox/conversations/{conversation_id}", data=payload
        )

    async def aget_inbox_conversation_messages(
        self, conversation_id: str, account_id: str
    ) -> dict[str, Any]:
        """Get messages in a conversation (async)"""
        params = self._build_params(
            account_id=account_id,
        )
        return await self._client._aget(
            f"/v1/inbox/conversations/{conversation_id}/messages", params=params
        )

    async def asend_inbox_message(
        self,
        conversation_id: str,
        account_id: str,
        *,
        message: str | None = None,
        quick_replies: list[dict[str, Any]] | None = None,
        buttons: list[dict[str, Any]] | None = None,
        template: dict[str, Any] | None = None,
        reply_markup: dict[str, Any] | None = None,
        messaging_type: str | None = None,
        message_tag: str | None = None,
        reply_to: str | None = None,
    ) -> dict[str, Any]:
        """Send a message (async)"""
        payload = self._build_payload(
            account_id=account_id,
            message=message,
            quick_replies=quick_replies,
            buttons=buttons,
            template=template,
            reply_markup=reply_markup,
            messaging_type=messaging_type,
            message_tag=message_tag,
            reply_to=reply_to,
        )
        return await self._client._apost(
            f"/v1/inbox/conversations/{conversation_id}/messages", data=payload
        )

    async def aedit_inbox_message(
        self,
        conversation_id: str,
        message_id: str,
        account_id: str,
        *,
        text: str | None = None,
        reply_markup: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Edit a message (Telegram only) (async)"""
        payload = self._build_payload(
            account_id=account_id,
            text=text,
            reply_markup=reply_markup,
        )
        return await self._client._apatch(
            f"/v1/inbox/conversations/{conversation_id}/messages/{message_id}",
            data=payload,
        )
