"""
Auto-generated ad_audiences 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 AdAudiencesResource:
    """
    ad_audiences 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_ad_audiences(self, account_id: str, ad_account_id: str, *, platform: str | None = None) -> dict[str, Any]:
        """List custom audiences"""
        params = self._build_params(
            account_id=account_id,
            ad_account_id=ad_account_id,
            platform=platform,
        )
        return self._client._get("/v1/ads/audiences", params=params)

    def create_ad_audience(self, account_id: str, ad_account_id: str, name: str, type: str, *, description: str | None = None, pixel_id: str | None = None, retention_days: int | None = None, source_audience_id: str | None = None, country: str | None = None, ratio: float | None = None, rule: dict[str, Any] | None = None, customer_file_source: str | None = None) -> dict[str, Any]:
        """Create a custom audience (Meta only)"""
        payload = self._build_payload(
            account_id=account_id,
            ad_account_id=ad_account_id,
            name=name,
            description=description,
            type=type,
            pixel_id=pixel_id,
            retention_days=retention_days,
            source_audience_id=source_audience_id,
            country=country,
            ratio=ratio,
            rule=rule,
            customer_file_source=customer_file_source,
        )
        return self._client._post("/v1/ads/audiences", data=payload)

    def get_ad_audience(self, audience_id: str) -> dict[str, Any]:
        """Get audience details"""
        return self._client._get(f"/v1/ads/audiences/{audience_id}")

    def delete_ad_audience(self, audience_id: str) -> dict[str, Any]:
        """Delete a custom audience"""
        return self._client._delete(f"/v1/ads/audiences/{audience_id}")

    def add_users_to_ad_audience(self, audience_id: str, users: list[dict[str, Any]]) -> dict[str, Any]:
        """Add users to a customer list audience"""
        payload = self._build_payload(
            users=users,
        )
        return self._client._post(f"/v1/ads/audiences/{audience_id}/users", data=payload)

    async def alist_ad_audiences(self, account_id: str, ad_account_id: str, *, platform: str | None = None) -> dict[str, Any]:
        """List custom audiences (async)"""
        params = self._build_params(
            account_id=account_id,
            ad_account_id=ad_account_id,
            platform=platform,
        )
        return await self._client._aget("/v1/ads/audiences", params=params)

    async def acreate_ad_audience(self, account_id: str, ad_account_id: str, name: str, type: str, *, description: str | None = None, pixel_id: str | None = None, retention_days: int | None = None, source_audience_id: str | None = None, country: str | None = None, ratio: float | None = None, rule: dict[str, Any] | None = None, customer_file_source: str | None = None) -> dict[str, Any]:
        """Create a custom audience (Meta only) (async)"""
        payload = self._build_payload(
            account_id=account_id,
            ad_account_id=ad_account_id,
            name=name,
            description=description,
            type=type,
            pixel_id=pixel_id,
            retention_days=retention_days,
            source_audience_id=source_audience_id,
            country=country,
            ratio=ratio,
            rule=rule,
            customer_file_source=customer_file_source,
        )
        return await self._client._apost("/v1/ads/audiences", data=payload)

    async def aget_ad_audience(self, audience_id: str) -> dict[str, Any]:
        """Get audience details (async)"""
        return await self._client._aget(f"/v1/ads/audiences/{audience_id}")

    async def adelete_ad_audience(self, audience_id: str) -> dict[str, Any]:
        """Delete a custom audience (async)"""
        return await self._client._adelete(f"/v1/ads/audiences/{audience_id}")

    async def aadd_users_to_ad_audience(self, audience_id: str, users: list[dict[str, Any]]) -> dict[str, Any]:
        """Add users to a customer list audience (async)"""
        payload = self._build_payload(
            users=users,
        )
        return await self._client._apost(f"/v1/ads/audiences/{audience_id}/users", data=payload)
