"""
Auto-generated webhooks 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 WebhooksResource:
    """
    Configure event webhooks.
    """

    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 get_webhook_settings(self) -> dict[str, Any]:
        """List webhooks"""
        return self._client._get("/v1/webhooks/settings")

    def create_webhook_settings(
        self,
        *,
        name: str | None = None,
        url: str | None = None,
        secret: str | None = None,
        events: list[str] | None = None,
        is_active: bool | None = None,
        custom_headers: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Create webhook"""
        payload = self._build_payload(
            name=name,
            url=url,
            secret=secret,
            events=events,
            is_active=is_active,
            custom_headers=custom_headers,
        )
        return self._client._post("/v1/webhooks/settings", data=payload)

    def update_webhook_settings(
        self,
        _id: str,
        *,
        name: str | None = None,
        url: str | None = None,
        secret: str | None = None,
        events: list[str] | None = None,
        is_active: bool | None = None,
        custom_headers: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Update webhook"""
        payload = self._build_payload(
            _id=_id,
            name=name,
            url=url,
            secret=secret,
            events=events,
            is_active=is_active,
            custom_headers=custom_headers,
        )
        return self._client._put("/v1/webhooks/settings", data=payload)

    def delete_webhook_settings(self, id: str) -> dict[str, Any]:
        """Delete webhook"""
        params = self._build_params(
            id=id,
        )
        return self._client._delete("/v1/webhooks/settings", params=params)

    def test_webhook(self, webhook_id: str) -> dict[str, Any]:
        """Send test webhook"""
        payload = self._build_payload(
            webhook_id=webhook_id,
        )
        return self._client._post("/v1/webhooks/test", data=payload)

    def get_webhook_logs(
        self,
        *,
        limit: int | None = 50,
        status: str | None = None,
        event: str | None = None,
        webhook_id: str | None = None,
    ) -> dict[str, Any]:
        """Get delivery logs"""
        params = self._build_params(
            limit=limit,
            status=status,
            event=event,
            webhook_id=webhook_id,
        )
        return self._client._get("/v1/webhooks/logs", params=params)

    async def aget_webhook_settings(self) -> dict[str, Any]:
        """List webhooks (async)"""
        return await self._client._aget("/v1/webhooks/settings")

    async def acreate_webhook_settings(
        self,
        *,
        name: str | None = None,
        url: str | None = None,
        secret: str | None = None,
        events: list[str] | None = None,
        is_active: bool | None = None,
        custom_headers: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Create webhook (async)"""
        payload = self._build_payload(
            name=name,
            url=url,
            secret=secret,
            events=events,
            is_active=is_active,
            custom_headers=custom_headers,
        )
        return await self._client._apost("/v1/webhooks/settings", data=payload)

    async def aupdate_webhook_settings(
        self,
        _id: str,
        *,
        name: str | None = None,
        url: str | None = None,
        secret: str | None = None,
        events: list[str] | None = None,
        is_active: bool | None = None,
        custom_headers: dict[str, Any] | None = None,
    ) -> dict[str, Any]:
        """Update webhook (async)"""
        payload = self._build_payload(
            _id=_id,
            name=name,
            url=url,
            secret=secret,
            events=events,
            is_active=is_active,
            custom_headers=custom_headers,
        )
        return await self._client._aput("/v1/webhooks/settings", data=payload)

    async def adelete_webhook_settings(self, id: str) -> dict[str, Any]:
        """Delete webhook (async)"""
        params = self._build_params(
            id=id,
        )
        return await self._client._adelete("/v1/webhooks/settings", params=params)

    async def atest_webhook(self, webhook_id: str) -> dict[str, Any]:
        """Send test webhook (async)"""
        payload = self._build_payload(
            webhook_id=webhook_id,
        )
        return await self._client._apost("/v1/webhooks/test", data=payload)

    async def aget_webhook_logs(
        self,
        *,
        limit: int | None = 50,
        status: str | None = None,
        event: str | None = None,
        webhook_id: str | None = None,
    ) -> dict[str, Any]:
        """Get delivery logs (async)"""
        params = self._build_params(
            limit=limit,
            status=status,
            event=event,
            webhook_id=webhook_id,
        )
        return await self._client._aget("/v1/webhooks/logs", params=params)
