"""
Auto-generated custom_fields 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 CustomFieldsResource:
    """
    custom_fields 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 set_contact_field_value(
        self, contact_id: str, slug: str, value: Any
    ) -> dict[str, Any]:
        """Set a custom field value"""
        payload = self._build_payload(
            value=value,
        )
        return self._client._put(
            f"/v1/contacts/{contact_id}/fields/{slug}", data=payload
        )

    def clear_contact_field_value(self, contact_id: str, slug: str) -> dict[str, Any]:
        """Clear a custom field value"""
        return self._client._delete(f"/v1/contacts/{contact_id}/fields/{slug}")

    def list_custom_fields(self, *, profile_id: str | None = None) -> dict[str, Any]:
        """List custom field definitions"""
        params = self._build_params(
            profile_id=profile_id,
        )
        return self._client._get("/v1/custom-fields", params=params)

    def create_custom_field(
        self,
        profile_id: str,
        name: str,
        type: str,
        *,
        slug: str | None = None,
        options: list[str] | None = None,
    ) -> dict[str, Any]:
        """Create a custom field definition"""
        payload = self._build_payload(
            profile_id=profile_id,
            name=name,
            slug=slug,
            type=type,
            options=options,
        )
        return self._client._post("/v1/custom-fields", data=payload)

    def update_custom_field(
        self,
        field_id: str,
        *,
        name: str | None = None,
        options: list[str] | None = None,
    ) -> dict[str, Any]:
        """Update a custom field definition"""
        payload = self._build_payload(
            name=name,
            options=options,
        )
        return self._client._patch(f"/v1/custom-fields/{field_id}", data=payload)

    def delete_custom_field(self, field_id: str) -> dict[str, Any]:
        """Delete a custom field definition"""
        return self._client._delete(f"/v1/custom-fields/{field_id}")

    async def aset_contact_field_value(
        self, contact_id: str, slug: str, value: Any
    ) -> dict[str, Any]:
        """Set a custom field value (async)"""
        payload = self._build_payload(
            value=value,
        )
        return await self._client._aput(
            f"/v1/contacts/{contact_id}/fields/{slug}", data=payload
        )

    async def aclear_contact_field_value(
        self, contact_id: str, slug: str
    ) -> dict[str, Any]:
        """Clear a custom field value (async)"""
        return await self._client._adelete(f"/v1/contacts/{contact_id}/fields/{slug}")

    async def alist_custom_fields(
        self, *, profile_id: str | None = None
    ) -> dict[str, Any]:
        """List custom field definitions (async)"""
        params = self._build_params(
            profile_id=profile_id,
        )
        return await self._client._aget("/v1/custom-fields", params=params)

    async def acreate_custom_field(
        self,
        profile_id: str,
        name: str,
        type: str,
        *,
        slug: str | None = None,
        options: list[str] | None = None,
    ) -> dict[str, Any]:
        """Create a custom field definition (async)"""
        payload = self._build_payload(
            profile_id=profile_id,
            name=name,
            slug=slug,
            type=type,
            options=options,
        )
        return await self._client._apost("/v1/custom-fields", data=payload)

    async def aupdate_custom_field(
        self,
        field_id: str,
        *,
        name: str | None = None,
        options: list[str] | None = None,
    ) -> dict[str, Any]:
        """Update a custom field definition (async)"""
        payload = self._build_payload(
            name=name,
            options=options,
        )
        return await self._client._apatch(f"/v1/custom-fields/{field_id}", data=payload)

    async def adelete_custom_field(self, field_id: str) -> dict[str, Any]:
        """Delete a custom field definition (async)"""
        return await self._client._adelete(f"/v1/custom-fields/{field_id}")
