"""FastAPI webhook helpers — requires ``pip install "0bit-gate[fastapi]"``."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from ..client import GateClient

if TYPE_CHECKING:
    from starlette.requests import Request


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


async def construct_event_from_request(
    client: GateClient,
    request: Request,
    secret: str,
    *,
    signature_header: str = "Gate-Signature",
) -> dict[str, Any]:
    """Verify a FastAPI/Starlette webhook request and return the parsed event."""
    _require_fastapi()
    raw = await request.body()
    return client.webhooks.construct_event(
        payload=raw,
        sig_header=request.headers.get(signature_header, ""),
        secret=secret,
    )


__all__ = ["construct_event_from_request"]
