"""
Auto-generated ad_campaigns 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 AdCampaignsResource:
    """
    ad_campaigns 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_campaigns(self, *, page: int | None = 1, limit: int | None = 20, source: str | None = "zernio", platform: str | None = None, status: str | None = None, ad_account_id: str | None = None, account_id: str | None = None, profile_id: str | None = None) -> dict[str, Any]:
        """List campaigns with aggregate metrics"""
        params = self._build_params(
            page=page,
            limit=limit,
            source=source,
            platform=platform,
            status=status,
            ad_account_id=ad_account_id,
            account_id=account_id,
            profile_id=profile_id,
        )
        return self._client._get("/v1/ads/campaigns", params=params)

    def update_ad_campaign_status(self, campaign_id: str, status: str, platform: str) -> dict[str, Any]:
        """Pause or resume a campaign"""
        payload = self._build_payload(
            status=status,
            platform=platform,
        )
        return self._client._put(f"/v1/ads/campaigns/{campaign_id}/status", data=payload)

    def get_ad_tree(self, *, page: int | None = 1, limit: int | None = 20, source: str | None = "zernio", platform: str | None = None, status: str | None = None, ad_account_id: str | None = None, account_id: str | None = None, profile_id: str | None = None) -> dict[str, Any]:
        """Get nested campaign/ad-set/ad tree"""
        params = self._build_params(
            page=page,
            limit=limit,
            source=source,
            platform=platform,
            status=status,
            ad_account_id=ad_account_id,
            account_id=account_id,
            profile_id=profile_id,
        )
        return self._client._get("/v1/ads/tree", params=params)

    async def alist_ad_campaigns(self, *, page: int | None = 1, limit: int | None = 20, source: str | None = "zernio", platform: str | None = None, status: str | None = None, ad_account_id: str | None = None, account_id: str | None = None, profile_id: str | None = None) -> dict[str, Any]:
        """List campaigns with aggregate metrics (async)"""
        params = self._build_params(
            page=page,
            limit=limit,
            source=source,
            platform=platform,
            status=status,
            ad_account_id=ad_account_id,
            account_id=account_id,
            profile_id=profile_id,
        )
        return await self._client._aget("/v1/ads/campaigns", params=params)

    async def aupdate_ad_campaign_status(self, campaign_id: str, status: str, platform: str) -> dict[str, Any]:
        """Pause or resume a campaign (async)"""
        payload = self._build_payload(
            status=status,
            platform=platform,
        )
        return await self._client._aput(f"/v1/ads/campaigns/{campaign_id}/status", data=payload)

    async def aget_ad_tree(self, *, page: int | None = 1, limit: int | None = 20, source: str | None = "zernio", platform: str | None = None, status: str | None = None, ad_account_id: str | None = None, account_id: str | None = None, profile_id: str | None = None) -> dict[str, Any]:
        """Get nested campaign/ad-set/ad tree (async)"""
        params = self._build_params(
            page=page,
            limit=limit,
            source=source,
            platform=platform,
            status=status,
            ad_account_id=ad_account_id,
            account_id=account_id,
            profile_id=profile_id,
        )
        return await self._client._aget("/v1/ads/tree", params=params)
