"""Django webhook helpers — requires ``pip install "0bit-gate[django]"``."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from ..client import GateClient

if TYPE_CHECKING:
    from django.http import HttpRequest


def _require_django() -> None:
    try:
        import django  # noqa: F401
    except ImportError as err:
        raise ImportError(
            'Django integration requires the django extra: pip install "0bit-gate[django]"'
        ) from err


def construct_event_from_request(
    client: GateClient,
    request: HttpRequest,
    secret: str,
    *,
    signature_header: str = "Gate-Signature",
) -> dict[str, Any]:
    """Verify a Django webhook request and return the parsed event."""
    _require_django()
    return client.webhooks.construct_event(
        payload=request.body,
        sig_header=request.headers.get(signature_header, ""),
        secret=secret,
    )


__all__ = ["construct_event_from_request"]
