"""Flask webhook helpers — requires ``pip install "0bit-gate[flask]"``."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from ..client import GateClient
from ..errors import WebhookSignatureError

if TYPE_CHECKING:
    from flask import Request


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


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


__all__ = ["construct_event_from_request", "WebhookSignatureError"]
