"""
Auto-generated contacts 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 ContactsResource:
    """
    contacts 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_contacts(
        self,
        *,
        profile_id: str | None = None,
        search: str | None = None,
        tag: str | None = None,
        platform: str | None = None,
        is_subscribed: str | None = None,
        limit: int | None = 50,
        skip: int | None = 0,
    ) -> dict[str, Any]:
        """List contacts"""
        params = self._build_params(
            profile_id=profile_id,
            search=search,
            tag=tag,
            platform=platform,
            is_subscribed=is_subscribed,
            limit=limit,
            skip=skip,
        )
        return self._client._get("/v1/contacts", params=params)

    def create_contact(
        self,
        profile_id: str,
        name: str,
        *,
        email: str | None = None,
        company: str | None = None,
        tags: list[str] | None = None,
        is_subscribed: bool | None = True,
        notes: str | None = None,
        account_id: str | None = None,
        platform: str | None = None,
        platform_identifier: str | None = None,
        display_identifier: str | None = None,
    ) -> dict[str, Any]:
        """Create a contact"""
        payload = self._build_payload(
            profile_id=profile_id,
            name=name,
            email=email,
            company=company,
            tags=tags,
            is_subscribed=is_subscribed,
            notes=notes,
            account_id=account_id,
            platform=platform,
            platform_identifier=platform_identifier,
            display_identifier=display_identifier,
        )
        return self._client._post("/v1/contacts", data=payload)

    def get_contact(self, contact_id: str) -> dict[str, Any]:
        """Get contact with channels"""
        return self._client._get(f"/v1/contacts/{contact_id}")

    def update_contact(
        self,
        contact_id: str,
        *,
        name: str | None = None,
        email: str | None = None,
        company: str | None = None,
        avatar_url: str | None = None,
        tags: list[str] | None = None,
        is_subscribed: bool | None = None,
        is_blocked: bool | None = None,
        notes: str | None = None,
    ) -> dict[str, Any]:
        """Update a contact"""
        payload = self._build_payload(
            name=name,
            email=email,
            company=company,
            avatar_url=avatar_url,
            tags=tags,
            is_subscribed=is_subscribed,
            is_blocked=is_blocked,
            notes=notes,
        )
        return self._client._patch(f"/v1/contacts/{contact_id}", data=payload)

    def delete_contact(self, contact_id: str) -> dict[str, Any]:
        """Delete a contact"""
        return self._client._delete(f"/v1/contacts/{contact_id}")

    def get_contact_channels(self, contact_id: str) -> dict[str, Any]:
        """List channels for a contact"""
        return self._client._get(f"/v1/contacts/{contact_id}/channels")

    def bulk_create_contacts(
        self,
        profile_id: str,
        account_id: str,
        platform: str,
        contacts: list[dict[str, Any]],
    ) -> dict[str, Any]:
        """Bulk create contacts"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform=platform,
            contacts=contacts,
        )
        return self._client._post("/v1/contacts/bulk", data=payload)

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

    async def acreate_contact(
        self,
        profile_id: str,
        name: str,
        *,
        email: str | None = None,
        company: str | None = None,
        tags: list[str] | None = None,
        is_subscribed: bool | None = True,
        notes: str | None = None,
        account_id: str | None = None,
        platform: str | None = None,
        platform_identifier: str | None = None,
        display_identifier: str | None = None,
    ) -> dict[str, Any]:
        """Create a contact (async)"""
        payload = self._build_payload(
            profile_id=profile_id,
            name=name,
            email=email,
            company=company,
            tags=tags,
            is_subscribed=is_subscribed,
            notes=notes,
            account_id=account_id,
            platform=platform,
            platform_identifier=platform_identifier,
            display_identifier=display_identifier,
        )
        return await self._client._apost("/v1/contacts", data=payload)

    async def aget_contact(self, contact_id: str) -> dict[str, Any]:
        """Get contact with channels (async)"""
        return await self._client._aget(f"/v1/contacts/{contact_id}")

    async def aupdate_contact(
        self,
        contact_id: str,
        *,
        name: str | None = None,
        email: str | None = None,
        company: str | None = None,
        avatar_url: str | None = None,
        tags: list[str] | None = None,
        is_subscribed: bool | None = None,
        is_blocked: bool | None = None,
        notes: str | None = None,
    ) -> dict[str, Any]:
        """Update a contact (async)"""
        payload = self._build_payload(
            name=name,
            email=email,
            company=company,
            avatar_url=avatar_url,
            tags=tags,
            is_subscribed=is_subscribed,
            is_blocked=is_blocked,
            notes=notes,
        )
        return await self._client._apatch(f"/v1/contacts/{contact_id}", data=payload)

    async def adelete_contact(self, contact_id: str) -> dict[str, Any]:
        """Delete a contact (async)"""
        return await self._client._adelete(f"/v1/contacts/{contact_id}")

    async def aget_contact_channels(self, contact_id: str) -> dict[str, Any]:
        """List channels for a contact (async)"""
        return await self._client._aget(f"/v1/contacts/{contact_id}/channels")

    async def abulk_create_contacts(
        self,
        profile_id: str,
        account_id: str,
        platform: str,
        contacts: list[dict[str, Any]],
    ) -> dict[str, Any]:
        """Bulk create contacts (async)"""
        payload = self._build_payload(
            profile_id=profile_id,
            account_id=account_id,
            platform=platform,
            contacts=contacts,
        )
        return await self._client._apost("/v1/contacts/bulk", data=payload)
