"""Artist API: overview aggregates + paged images."""

from quart import Blueprint, jsonify, request

from ..extensions import get_session
from ..services.artist_service import ArtistService

artist_bp = Blueprint("artist", __name__, url_prefix="/api/artist")


@artist_bp.route("/<slug>", methods=["GET"])
async def overview(slug: str):
    async with get_session() as session:
        svc = ArtistService(session)
        data = await svc.overview(slug)
    if data is None:
        return jsonify({"error": "artist not found"}), 404
    return jsonify(data)


@artist_bp.route("/<slug>/images", methods=["GET"])
async def images(slug: str):
    cursor = request.args.get("cursor") or None
    try:
        limit = int(request.args.get("limit", "60"))
    except ValueError:
        return jsonify({"error": "limit must be an integer"}), 400
    async with get_session() as session:
        svc = ArtistService(session)
        try:
            page = await svc.images(slug, cursor=cursor, limit=limit)
        except ValueError as exc:
            return jsonify({"error": str(exc)}), 400
    if page is None:
        return jsonify({"error": "artist not found"}), 404
    return jsonify({"images": page.images, "next_cursor": page.next_cursor})
