# This file was generated by liblab | https://liblab.com/

from .utils.validator import Validator
from .utils.base_service import BaseService
from ..net.transport.serializer import Serializer
from ..models.webhook_list_response import WebhookListResponse
from ..models.webhook_item_response import WebhookItemResponse
from ..models.webhook_event_type_list_response import WebhookEventTypeListResponse
from ..models.utils.cast_models import cast_models
from ..models.patch_webhook_request import PatchWebhookRequest
from ..models.create_webhook_request import CreateWebhookRequest


class WebhooksService(BaseService):

    @cast_models
    def get_all_webhooks(self) -> WebhookListResponse:
        """Retrieve a list of webhook subscriptions.

        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: successful operation
        :rtype: WebhookListResponse
        """

        serialized_request = (
            Serializer(f"{self.base_url}/webhooks", self.get_default_headers())
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return WebhookListResponse._unmap(response)

    @cast_models
    def create_webhook(self, request_body: CreateWebhookRequest) -> WebhookItemResponse:
        """Create a new webhooks subscription.

        :param request_body: The request body.
        :type request_body: CreateWebhookRequest
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: successful operation
        :rtype: WebhookItemResponse
        """

        Validator(CreateWebhookRequest).validate(request_body)

        serialized_request = (
            Serializer(f"{self.base_url}/webhooks", self.get_default_headers())
            .serialize()
            .set_method("POST")
            .set_body(request_body)
        )

        response = self.send_request(serialized_request)

        return WebhookItemResponse._unmap(response)

    @cast_models
    def webhook_controller_get_by_id(self, id_: str) -> WebhookItemResponse:
        """Retrieve a single webhook subscription.

        :param id_: id_
        :type id_: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: successful operation
        :rtype: WebhookItemResponse
        """

        Validator(str).validate(id_)

        serialized_request = (
            Serializer(f"{self.base_url}/webhooks/{{id}}", self.get_default_headers())
            .add_path("id", id_)
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return WebhookItemResponse._unmap(response)

    @cast_models
    def webhook_controller_edit_by_id(
        self, request_body: PatchWebhookRequest, id_: str
    ) -> WebhookItemResponse:
        """Edit a webhook subscription.

        :param request_body: The request body.
        :type request_body: PatchWebhookRequest
        :param id_: id_
        :type id_: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: successful operation
        :rtype: WebhookItemResponse
        """

        Validator(PatchWebhookRequest).validate(request_body)
        Validator(str).validate(id_)

        serialized_request = (
            Serializer(f"{self.base_url}/webhooks/{{id}}", self.get_default_headers())
            .add_path("id", id_)
            .serialize()
            .set_method("PATCH")
            .set_body(request_body)
        )

        response = self.send_request(serialized_request)

        return WebhookItemResponse._unmap(response)

    @cast_models
    def webhook_controller_delete_by_id(self, id_: str):
        """Delete a webhook subscription.

        :param id_: id_
        :type id_: str
        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        """

        Validator(str).validate(id_)

        serialized_request = (
            Serializer(f"{self.base_url}/webhooks/{{id}}", self.get_default_headers())
            .add_path("id", id_)
            .serialize()
            .set_method("DELETE")
        )

        response = self.send_request(serialized_request)

        return response

    @cast_models
    def get_all_webhook_event_types(self) -> WebhookEventTypeListResponse:
        """Retrieve a list of webhook event types.

        ...
        :raises RequestError: Raised when a request fails, with optional HTTP status code and details.
        ...
        :return: successful operation
        :rtype: WebhookEventTypeListResponse
        """

        serialized_request = (
            Serializer(
                f"{self.base_url}/webhooks/events/types", self.get_default_headers()
            )
            .serialize()
            .set_method("GET")
        )

        response = self.send_request(serialized_request)

        return WebhookEventTypeListResponse._unmap(response)
