"""
Request validation decorators for Flask route handlers.
"""

from functools import wraps

from flask import jsonify, request


def require_json_body(
    *,
    error_format="simple",
    error_message="Request body must be valid JSON",
):
    """Decorator that validates request body is a JSON dict.

    Returns 400 if the parsed body is not a ``dict``.  The decorated function
    is responsible for calling ``request.get_json()`` (or ``request.json``)
    itself — Flask caches the result so there is no duplicate parsing.

    Args:
        error_format: Response format on failure.
            ``"simple"``  → ``{"error": msg}``
            ``"status"``  → ``{"status": "error", "message": msg}``
            ``"success"`` → ``{"success": False, "error": msg}``
        error_message: The error message string.
    """

    def decorator(fn):
        @wraps(fn)
        def wrapper(*args, **kwargs):
            data = request.get_json(silent=True)
            if not isinstance(data, dict):
                if error_format == "status":
                    return (
                        jsonify({"status": "error", "message": error_message}),
                        400,
                    )
                if error_format == "success":
                    return (
                        jsonify({"success": False, "error": error_message}),
                        400,
                    )
                # "simple"
                return jsonify({"error": error_message}), 400
            return fn(*args, **kwargs)

        return wrapper

    return decorator
