"""
Auto-generated broadcasts 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 datetime import datetime

    from ..client.base import BaseClient


class BroadcastsResource:
    """
    broadcasts 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_broadcasts(
        self,
        *,
        profile_id: str | None = None,
        status: str | None = None,
        platform: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List broadcasts"""
        params = self._build_params(
            profile_id=profile_id,
            status=status,
            platform=platform,
            limit=limit,
            skip=skip,
        )
        return self._client._get("/v1/broadcasts", params=params)

    def create_broadcast(
        self,
        profile_id: str,
        account_id: str,
        platform: str,
        name: str,
        *,
        description: str | None = None,
        message: dict[str, Any] | None = None,
        template: dict[str, Any] | None = None,
        segment_filters: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Create a broadcast draft"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform=platform,
            name=name,
            description=description,
            message=message,
            template=template,
            segment_filters=segment_filters,
        )
        return self._client._post("/v1/broadcasts", data=payload)

    def get_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Get broadcast details"""
        return self._client._get(f"/v1/broadcasts/{broadcast_id}")

    def update_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Update a broadcast"""
        return self._client._patch(f"/v1/broadcasts/{broadcast_id}")

    def delete_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Delete a broadcast (draft only)"""
        return self._client._delete(f"/v1/broadcasts/{broadcast_id}")

    def send_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Trigger immediate send"""
        return self._client._post(f"/v1/broadcasts/{broadcast_id}/send")

    def schedule_broadcast(
        self, broadcast_id: str, scheduled_at: datetime | str
    ) -> dict[str, Any]:
        """Schedule broadcast for later"""
        payload = self._build_payload(
            scheduled_at=scheduled_at,
        )
        return self._client._post(
            f"/v1/broadcasts/{broadcast_id}/schedule", data=payload
        )

    def cancel_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Cancel a broadcast"""
        return self._client._post(f"/v1/broadcasts/{broadcast_id}/cancel")

    def list_broadcast_recipients(
        self,
        broadcast_id: str,
        *,
        status: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List broadcast recipients"""
        params = self._build_params(
            status=status,
            limit=limit,
            skip=skip,
        )
        return self._client._get(
            f"/v1/broadcasts/{broadcast_id}/recipients", params=params
        )

    def add_broadcast_recipients(
        self,
        broadcast_id: str,
        *,
        contact_ids: list[str] | None = None,
        phones: list[str] | None = None,
        use_segment: bool | None = None,
    ) -> dict[str, Any]:
        """Add recipients to a broadcast"""
        payload = self._build_payload(
            contact_ids=contact_ids,
            phones=phones,
            use_segment=use_segment,
        )
        return self._client._post(
            f"/v1/broadcasts/{broadcast_id}/recipients", data=payload
        )

    async def alist_broadcasts(
        self,
        *,
        profile_id: str | None = None,
        status: str | None = None,
        platform: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List broadcasts (async)"""
        params = self._build_params(
            profile_id=profile_id,
            status=status,
            platform=platform,
            limit=limit,
            skip=skip,
        )
        return await self._client._aget("/v1/broadcasts", params=params)

    async def acreate_broadcast(
        self,
        profile_id: str,
        account_id: str,
        platform: str,
        name: str,
        *,
        description: str | None = None,
        message: dict[str, Any] | None = None,
        template: dict[str, Any] | None = None,
        segment_filters: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Create a broadcast draft (async)"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform=platform,
            name=name,
            description=description,
            message=message,
            template=template,
            segment_filters=segment_filters,
        )
        return await self._client._apost("/v1/broadcasts", data=payload)

    async def aget_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Get broadcast details (async)"""
        return await self._client._aget(f"/v1/broadcasts/{broadcast_id}")

    async def aupdate_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Update a broadcast (async)"""
        return await self._client._apatch(f"/v1/broadcasts/{broadcast_id}")

    async def adelete_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Delete a broadcast (draft only) (async)"""
        return await self._client._adelete(f"/v1/broadcasts/{broadcast_id}")

    async def asend_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Trigger immediate send (async)"""
        return await self._client._apost(f"/v1/broadcasts/{broadcast_id}/send")

    async def aschedule_broadcast(
        self, broadcast_id: str, scheduled_at: datetime | str
    ) -> dict[str, Any]:
        """Schedule broadcast for later (async)"""
        payload = self._build_payload(
            scheduled_at=scheduled_at,
        )
        return await self._client._apost(
            f"/v1/broadcasts/{broadcast_id}/schedule", data=payload
        )

    async def acancel_broadcast(self, broadcast_id: str) -> dict[str, Any]:
        """Cancel a broadcast (async)"""
        return await self._client._apost(f"/v1/broadcasts/{broadcast_id}/cancel")

    async def alist_broadcast_recipients(
        self,
        broadcast_id: str,
        *,
        status: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List broadcast recipients (async)"""
        params = self._build_params(
            status=status,
            limit=limit,
            skip=skip,
        )
        return await self._client._aget(
            f"/v1/broadcasts/{broadcast_id}/recipients", params=params
        )

    async def aadd_broadcast_recipients(
        self,
        broadcast_id: str,
        *,
        contact_ids: list[str] | None = None,
        phones: list[str] | None = None,
        use_segment: bool | None = None,
    ) -> dict[str, Any]:
        """Add recipients to a broadcast (async)"""
        payload = self._build_payload(
            contact_ids=contact_ids,
            phones=phones,
            use_segment=use_segment,
        )
        return await self._client._apost(
            f"/v1/broadcasts/{broadcast_id}/recipients", data=payload
        )
