from __future__ import annotations

from typing import Any, Dict, List, TYPE_CHECKING

if TYPE_CHECKING:
    from socialrails.client import SocialRails, AsyncSocialRails


class Webhooks:
    """Synchronous resource for managing webhooks."""

    def __init__(self, client: SocialRails) -> None:
        self._client = client

    def list(self) -> List[Dict[str, Any]]:
        """List all registered webhooks.

        Returns:
            List of webhook objects with id, url, events, is_active, and created_at.
        """
        return self._client._get("/webhooks")

    def create(self, url: str, events: List[str]) -> Dict[str, Any]:
        """Register a new webhook.

        Args:
            url: The HTTPS URL to receive webhook events.
            events: List of event types to subscribe to.
                Valid events: "post.published", "post.failed", "post.scheduled".

        Returns:
            The created webhook including the signing secret.
        """
        return self._client._post("/webhooks", {"url": url, "events": events})

    def delete(self, webhook_id: str) -> Dict[str, Any]:
        """Delete a webhook.

        Args:
            webhook_id: The UUID of the webhook to delete.

        Returns:
            Dict with "id" and "deleted" confirmation.
        """
        return self._client._delete(f"/webhooks/{webhook_id}")


class AsyncWebhooks:
    """Asynchronous resource for managing webhooks."""

    def __init__(self, client: AsyncSocialRails) -> None:
        self._client = client

    async def list(self) -> List[Dict[str, Any]]:
        """List all registered webhooks.

        Returns:
            List of webhook objects with id, url, events, is_active, and created_at.
        """
        return await self._client._get("/webhooks")

    async def create(self, url: str, events: List[str]) -> Dict[str, Any]:
        """Register a new webhook.

        Args:
            url: The HTTPS URL to receive webhook events.
            events: List of event types to subscribe to.
                Valid events: "post.published", "post.failed", "post.scheduled".

        Returns:
            The created webhook including the signing secret.
        """
        return await self._client._post("/webhooks", {"url": url, "events": events})

    async def delete(self, webhook_id: str) -> Dict[str, Any]:
        """Delete a webhook.

        Args:
            webhook_id: The UUID of the webhook to delete.

        Returns:
            Dict with "id" and "deleted" confirmation.
        """
        return await self._client._delete(f"/webhooks/{webhook_id}")
