"""
Auto-generated sequences 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 SequencesResource:
    """
    sequences 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_sequences(
        self,
        *,
        profile_id: str | None = None,
        status: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List sequences"""
        params = self._build_params(
            profile_id=profile_id,
            status=status,
            limit=limit,
            skip=skip,
        )
        return self._client._get("/v1/sequences", params=params)

    def create_sequence(
        self,
        profile_id: str,
        account_id: str,
        platform: str,
        name: str,
        *,
        description: str | None = None,
        steps: list[dict[str, Any]] | None = None,
        exit_on_reply: bool | None = True,
        exit_on_unsubscribe: bool | None = True,
    ) -> dict[str, Any]:
        """Create a sequence"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform=platform,
            name=name,
            description=description,
            steps=steps,
            exit_on_reply=exit_on_reply,
            exit_on_unsubscribe=exit_on_unsubscribe,
        )
        return self._client._post("/v1/sequences", data=payload)

    def get_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Get sequence with steps"""
        return self._client._get(f"/v1/sequences/{sequence_id}")

    def update_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Update a sequence"""
        return self._client._patch(f"/v1/sequences/{sequence_id}")

    def delete_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Delete a sequence"""
        return self._client._delete(f"/v1/sequences/{sequence_id}")

    def activate_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Activate a sequence"""
        return self._client._post(f"/v1/sequences/{sequence_id}/activate")

    def pause_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Pause a sequence"""
        return self._client._post(f"/v1/sequences/{sequence_id}/pause")

    def enroll_contacts(
        self,
        sequence_id: str,
        contact_ids: list[str],
        *,
        channel_ids: list[str] | None = None,
    ) -> dict[str, Any]:
        """Enroll contacts in a sequence"""
        payload = self._build_payload(
            contact_ids=contact_ids,
            channel_ids=channel_ids,
        )
        return self._client._post(f"/v1/sequences/{sequence_id}/enroll", data=payload)

    def unenroll_contact(self, sequence_id: str, contact_id: str) -> dict[str, Any]:
        """Unenroll a contact from a sequence"""
        return self._client._delete(f"/v1/sequences/{sequence_id}/enroll/{contact_id}")

    def list_sequence_enrollments(
        self,
        sequence_id: str,
        *,
        status: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List enrollments for a sequence"""
        params = self._build_params(
            status=status,
            limit=limit,
            skip=skip,
        )
        return self._client._get(
            f"/v1/sequences/{sequence_id}/enrollments", params=params
        )

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

    async def acreate_sequence(
        self,
        profile_id: str,
        account_id: str,
        platform: str,
        name: str,
        *,
        description: str | None = None,
        steps: list[dict[str, Any]] | None = None,
        exit_on_reply: bool | None = True,
        exit_on_unsubscribe: bool | None = True,
    ) -> dict[str, Any]:
        """Create a sequence (async)"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform=platform,
            name=name,
            description=description,
            steps=steps,
            exit_on_reply=exit_on_reply,
            exit_on_unsubscribe=exit_on_unsubscribe,
        )
        return await self._client._apost("/v1/sequences", data=payload)

    async def aget_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Get sequence with steps (async)"""
        return await self._client._aget(f"/v1/sequences/{sequence_id}")

    async def aupdate_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Update a sequence (async)"""
        return await self._client._apatch(f"/v1/sequences/{sequence_id}")

    async def adelete_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Delete a sequence (async)"""
        return await self._client._adelete(f"/v1/sequences/{sequence_id}")

    async def aactivate_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Activate a sequence (async)"""
        return await self._client._apost(f"/v1/sequences/{sequence_id}/activate")

    async def apause_sequence(self, sequence_id: str) -> dict[str, Any]:
        """Pause a sequence (async)"""
        return await self._client._apost(f"/v1/sequences/{sequence_id}/pause")

    async def aenroll_contacts(
        self,
        sequence_id: str,
        contact_ids: list[str],
        *,
        channel_ids: list[str] | None = None,
    ) -> dict[str, Any]:
        """Enroll contacts in a sequence (async)"""
        payload = self._build_payload(
            contact_ids=contact_ids,
            channel_ids=channel_ids,
        )
        return await self._client._apost(
            f"/v1/sequences/{sequence_id}/enroll", data=payload
        )

    async def aunenroll_contact(
        self, sequence_id: str, contact_id: str
    ) -> dict[str, Any]:
        """Unenroll a contact from a sequence (async)"""
        return await self._client._adelete(
            f"/v1/sequences/{sequence_id}/enroll/{contact_id}"
        )

    async def alist_sequence_enrollments(
        self,
        sequence_id: str,
        *,
        status: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List enrollments for a sequence (async)"""
        params = self._build_params(
            status=status,
            limit=limit,
            skip=skip,
        )
        return await self._client._aget(
            f"/v1/sequences/{sequence_id}/enrollments", params=params
        )
