"""Provenance API: image -> posts, post -> header payload.

Separate from the gallery/tag APIs by design (provenance is its own
system). Read-only.
"""

from quart import Blueprint, jsonify

from ..extensions import get_session
from ..services.provenance_service import ProvenanceService

provenance_bp = Blueprint("provenance", __name__,
                          url_prefix="/api/provenance")


@provenance_bp.route("/image/<int:image_id>", methods=["GET"])
async def image_provenance(image_id: int):
    async with get_session() as session:
        svc = ProvenanceService(session)
        payload = await svc.for_image(image_id)
    if payload is None:
        return jsonify({"error": "not found"}), 404
    return jsonify(payload)


@provenance_bp.route("/post/<int:post_id>", methods=["GET"])
async def post_provenance(post_id: int):
    async with get_session() as session:
        svc = ProvenanceService(session)
        payload = await svc.for_post(post_id)
    if payload is None:
        return jsonify({"error": "not found"}), 404
    return jsonify(payload)
