"""Schema endpoint for inter-service protocol introspection.

Every service exposes ``GET /internal/schema`` which returns a JSON
manifest of all defquery and defaction definitions with their parameter
signatures and docstrings.

Usage::

    from shared.infrastructure.schema_blueprint import create_schema_blueprint
    app.register_blueprint(create_schema_blueprint("events"))
"""

from __future__ import annotations

from quart import Blueprint, jsonify, request


def create_schema_blueprint(service_name: str) -> Blueprint:
    """Create a blueprint exposing ``/internal/schema``."""
    bp = Blueprint(
        f"schema_{service_name}",
        __name__,
        url_prefix="/internal",
    )

    @bp.get("/schema")
    async def get_schema():
        from shared.sx.query_registry import schema_for_service
        return jsonify(schema_for_service(service_name))

    return bp
