"""
Auto-generated whatsapp_flows 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 WhatsappFlowsResource:
    """
    whatsapp_flows 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_whats_app_flows(self, account_id: str) -> dict[str, Any]:
        """List flows"""
        params = self._build_params(
            account_id=account_id,
        )
        return self._client._get("/v1/whatsapp/flows", params=params)

    def create_whats_app_flow(
        self,
        account_id: str,
        name: str,
        categories: list[str],
        *,
        clone_flow_id: str | None = None,
    ) -> dict[str, Any]:
        """Create flow"""
        payload = self._build_payload(
            account_id=account_id,
            name=name,
            categories=categories,
            clone_flow_id=clone_flow_id,
        )
        return self._client._post("/v1/whatsapp/flows", data=payload)

    def get_whats_app_flow(
        self, flow_id: str, account_id: str, *, fields: str | None = None
    ) -> dict[str, Any]:
        """Get flow"""
        params = self._build_params(
            account_id=account_id,
            fields=fields,
        )
        return self._client._get(f"/v1/whatsapp/flows/{flow_id}", params=params)

    def update_whats_app_flow(
        self,
        flow_id: str,
        account_id: str,
        *,
        name: str | None = None,
        categories: list[str] | None = None,
    ) -> dict[str, Any]:
        """Update flow"""
        payload = self._build_payload(
            account_id=account_id,
            name=name,
            categories=categories,
        )
        return self._client._patch(f"/v1/whatsapp/flows/{flow_id}", data=payload)

    def delete_whats_app_flow(self, flow_id: str, account_id: str) -> dict[str, Any]:
        """Delete flow"""
        params = self._build_params(
            account_id=account_id,
        )
        return self._client._delete(f"/v1/whatsapp/flows/{flow_id}", params=params)

    def get_whats_app_flow_json(self, flow_id: str, account_id: str) -> dict[str, Any]:
        """Get flow JSON asset"""
        params = self._build_params(
            account_id=account_id,
        )
        return self._client._get(f"/v1/whatsapp/flows/{flow_id}/json", params=params)

    def upload_whats_app_flow_json(
        self, flow_id: str, account_id: str, flow_json: Any
    ) -> dict[str, Any]:
        """Upload flow JSON"""
        payload = self._build_payload(
            account_id=account_id,
            flow_json=flow_json,
        )
        return self._client._put(f"/v1/whatsapp/flows/{flow_id}/json", data=payload)

    def publish_whats_app_flow(self, flow_id: str, account_id: str) -> dict[str, Any]:
        """Publish flow"""
        payload = self._build_payload(
            account_id=account_id,
        )
        return self._client._post(f"/v1/whatsapp/flows/{flow_id}/publish", data=payload)

    def deprecate_whats_app_flow(self, flow_id: str, account_id: str) -> dict[str, Any]:
        """Deprecate flow"""
        payload = self._build_payload(
            account_id=account_id,
        )
        return self._client._post(
            f"/v1/whatsapp/flows/{flow_id}/deprecate", data=payload
        )

    def send_whats_app_flow_message(
        self,
        account_id: str,
        to: str,
        flow_id: str,
        flow_cta: str,
        body: str,
        *,
        flow_action: str | None = "navigate",
        flow_token: str | None = None,
        flow_action_payload: dict[str, Any] | None = None,
        header: dict[str, Any] | None = None,
        footer: str | None = None,
        draft: bool | None = None,
    ) -> dict[str, Any]:
        """Send flow message"""
        payload = self._build_payload(
            account_id=account_id,
            to=to,
            flow_id=flow_id,
            flow_cta=flow_cta,
            flow_action=flow_action,
            flow_token=flow_token,
            flow_action_payload=flow_action_payload,
            body=body,
            header=header,
            footer=footer,
            draft=draft,
        )
        return self._client._post("/v1/whatsapp/flows/send", data=payload)

    async def alist_whats_app_flows(self, account_id: str) -> dict[str, Any]:
        """List flows (async)"""
        params = self._build_params(
            account_id=account_id,
        )
        return await self._client._aget("/v1/whatsapp/flows", params=params)

    async def acreate_whats_app_flow(
        self,
        account_id: str,
        name: str,
        categories: list[str],
        *,
        clone_flow_id: str | None = None,
    ) -> dict[str, Any]:
        """Create flow (async)"""
        payload = self._build_payload(
            account_id=account_id,
            name=name,
            categories=categories,
            clone_flow_id=clone_flow_id,
        )
        return await self._client._apost("/v1/whatsapp/flows", data=payload)

    async def aget_whats_app_flow(
        self, flow_id: str, account_id: str, *, fields: str | None = None
    ) -> dict[str, Any]:
        """Get flow (async)"""
        params = self._build_params(
            account_id=account_id,
            fields=fields,
        )
        return await self._client._aget(f"/v1/whatsapp/flows/{flow_id}", params=params)

    async def aupdate_whats_app_flow(
        self,
        flow_id: str,
        account_id: str,
        *,
        name: str | None = None,
        categories: list[str] | None = None,
    ) -> dict[str, Any]:
        """Update flow (async)"""
        payload = self._build_payload(
            account_id=account_id,
            name=name,
            categories=categories,
        )
        return await self._client._apatch(f"/v1/whatsapp/flows/{flow_id}", data=payload)

    async def adelete_whats_app_flow(
        self, flow_id: str, account_id: str
    ) -> dict[str, Any]:
        """Delete flow (async)"""
        params = self._build_params(
            account_id=account_id,
        )
        return await self._client._adelete(
            f"/v1/whatsapp/flows/{flow_id}", params=params
        )

    async def aget_whats_app_flow_json(
        self, flow_id: str, account_id: str
    ) -> dict[str, Any]:
        """Get flow JSON asset (async)"""
        params = self._build_params(
            account_id=account_id,
        )
        return await self._client._aget(
            f"/v1/whatsapp/flows/{flow_id}/json", params=params
        )

    async def aupload_whats_app_flow_json(
        self, flow_id: str, account_id: str, flow_json: Any
    ) -> dict[str, Any]:
        """Upload flow JSON (async)"""
        payload = self._build_payload(
            account_id=account_id,
            flow_json=flow_json,
        )
        return await self._client._aput(
            f"/v1/whatsapp/flows/{flow_id}/json", data=payload
        )

    async def apublish_whats_app_flow(
        self, flow_id: str, account_id: str
    ) -> dict[str, Any]:
        """Publish flow (async)"""
        payload = self._build_payload(
            account_id=account_id,
        )
        return await self._client._apost(
            f"/v1/whatsapp/flows/{flow_id}/publish", data=payload
        )

    async def adeprecate_whats_app_flow(
        self, flow_id: str, account_id: str
    ) -> dict[str, Any]:
        """Deprecate flow (async)"""
        payload = self._build_payload(
            account_id=account_id,
        )
        return await self._client._apost(
            f"/v1/whatsapp/flows/{flow_id}/deprecate", data=payload
        )

    async def asend_whats_app_flow_message(
        self,
        account_id: str,
        to: str,
        flow_id: str,
        flow_cta: str,
        body: str,
        *,
        flow_action: str | None = "navigate",
        flow_token: str | None = None,
        flow_action_payload: dict[str, Any] | None = None,
        header: dict[str, Any] | None = None,
        footer: str | None = None,
        draft: bool | None = None,
    ) -> dict[str, Any]:
        """Send flow message (async)"""
        payload = self._build_payload(
            account_id=account_id,
            to=to,
            flow_id=flow_id,
            flow_cta=flow_cta,
            flow_action=flow_action,
            flow_token=flow_token,
            flow_action_payload=flow_action_payload,
            body=body,
            header=header,
            footer=footer,
            draft=draft,
        )
        return await self._client._apost("/v1/whatsapp/flows/send", data=payload)
