from __future__ import annotations

from typing import TYPE_CHECKING

from ..objects.webhook import Webhook
from ..objects.list_response import ListResponse

if TYPE_CHECKING:
    from ..connection import Connection


class Webhooks:
    def __init__(self, *, connection: Connection):
        self._connection = connection

    def list(self) -> ListResponse[Webhook]:
        response = self._connection.get("/webhooks")
        data = [Webhook(w) for w in (response.get("data") or [])]
        return ListResponse(data=data, meta=response.get("meta", {}))

    def create(self, **params) -> Webhook:
        response = self._connection.post("/webhooks", {"webhook": params})
        return Webhook(response["data"])

    def retrieve(self, id: str) -> Webhook:
        response = self._connection.get(f"/webhooks/{id}")
        return Webhook(response["data"])

    def update(self, id: str, **params) -> Webhook:
        response = self._connection.patch(f"/webhooks/{id}", {"webhook": params})
        return Webhook(response["data"])

    def delete(self, id: str) -> bool:
        self._connection.delete(f"/webhooks/{id}")
        return True
