"""Tags API: autocomplete, create, list/add/remove for an image."""

from quart import Blueprint, jsonify, request
from sqlalchemy import exists, select
from sqlalchemy.exc import IntegrityError

from ..extensions import get_session
from ..models import Tag, TagKind
from ..models.tag_allowlist import TagAllowlist
from ..services.bulk_tag_service import BulkTagService
from ..services.ml.aliases import AliasService
from ..services.series_match_service import SeriesMatchService
from ..services.series_service import SeriesError, SeriesService
from ..services.tag_directory_service import TagDirectoryService
from ..services.tag_query import serialize_tag
from ..services.tag_service import (
    TagMergeConflict,
    TagService,
    TagValidationError,
    normalize_tag_name,
)
from ..utils.tag_prefix import parse_kind_prefix

tags_bp = Blueprint("tags", __name__, url_prefix="/api")


def _coerce_kind(raw: str | None) -> TagKind | None:
    if raw is None:
        return None
    try:
        return TagKind(raw)
    except ValueError:
        return None


def _parse_bulk_ids(
    body, *, max_ids: int = 200
) -> tuple[list[int] | None, tuple | None]:
    """Returns (image_ids, error). error is (json, status) or None."""
    if not body or "image_ids" not in body:
        return None, (jsonify({"error": "image_ids required"}), 400)
    raw = body["image_ids"]
    if not isinstance(raw, list) or not raw:
        return None, (
            jsonify({"error": "image_ids must be a non-empty list"}),
            400,
        )
    try:
        ids = [int(x) for x in raw]
    except (TypeError, ValueError):
        return None, (
            jsonify({"error": "image_ids must be integers"}),
            400,
        )
    if len(ids) > max_ids:
        return None, (
            jsonify({"error": f"selection too large (max {max_ids})"}),
            400,
        )
    return ids, None


@tags_bp.route("/tags/autocomplete", methods=["GET"])
async def autocomplete():
    q = request.args.get("q", "")
    kind = _coerce_kind(request.args.get("kind"))
    try:
        limit = int(request.args.get("limit", "20"))
    except ValueError:
        return jsonify({"error": "limit must be an integer"}), 400

    async with get_session() as session:
        svc = TagService(session)
        hits = await svc.autocomplete(q, kind=kind, limit=limit)

    return jsonify(
        [{**serialize_tag(h), "image_count": h.image_count} for h in hits]
    )


@tags_bp.route("/tags/directory", methods=["GET"])
async def directory():
    kind = request.args.get("kind") or None
    if kind is not None and _coerce_kind(kind) is None:
        return jsonify({"error": f"invalid kind {kind!r}"}), 400
    q = request.args.get("q") or None
    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 = TagDirectoryService(session)
        try:
            page = await svc.list_tags(kind=kind, q=q, cursor=cursor, limit=limit)
        except ValueError as exc:
            return jsonify({"error": str(exc)}), 400
    return jsonify({"cards": page.cards, "next_cursor": page.next_cursor})


@tags_bp.route("/tags", methods=["POST"])
async def create_tag():
    """Create a tag. Two input shapes accepted:
      1. Explicit: {name, kind, fandom_id?} — caller already split, kind wins.
      2. IR-suffix: {name} where name = "kind:Name" (e.g. "artist:Eric").
         The server runs parse_kind_prefix(name) to derive kind; the colon
         and prefix are stripped from the stored tag name. If no recognized
         prefix is present, the kind defaults to `general`.
    Explicit kind ALWAYS wins (backward-compat for existing callers).
    """
    body = await request.get_json()
    if not body or "name" not in body:
        return jsonify({"error": "name required"}), 400
    name = body["name"]
    explicit_kind_raw = body.get("kind")

    if explicit_kind_raw is not None:
        # Caller provided kind — honor it; don't re-parse.
        kind = _coerce_kind(explicit_kind_raw)
        if kind is None:
            return jsonify({"error": f"invalid kind {explicit_kind_raw!r}"}), 400
    else:
        # IR-style: parse "kind:Name" from the raw name.
        parsed_kind, parsed_name = parse_kind_prefix(name)
        if parsed_kind is not None:
            name = parsed_name
            kind = _coerce_kind(parsed_kind)
            # parse_kind_prefix only returns kinds from KNOWN_KINDS which
            # are all valid TagKind members, so _coerce_kind can't return
            # None here — but defensive.
            if kind is None:
                return jsonify({"error": f"invalid kind {parsed_kind!r}"}), 400
        else:
            kind = TagKind.general

    fandom_id = body.get("fandom_id")

    # #701: Title-Case operator-entered tags. Only here (the explicit create
    # endpoint), NOT in the shared find_or_create — the ML tagger uses that path
    # and must keep the booru vocabulary's casing for allowlist matching.
    name = normalize_tag_name(name)

    async with get_session() as session:
        svc = TagService(session)
        try:
            tag = await svc.find_or_create(name, kind, fandom_id=fandom_id)
        except TagValidationError as exc:
            return jsonify({"error": str(exc)}), 400
        await session.commit()
        return jsonify(
            {"id": tag.id, "name": tag.name, "kind": tag.kind.value, "fandom_id": tag.fandom_id}
        ), 201


@tags_bp.route("/images/<int:image_id>/tags", methods=["GET"])
async def list_tags_for_image(image_id: int):
    async with get_session() as session:
        svc = TagService(session)
        tags = await svc.list_for_image(image_id)
    return jsonify([serialize_tag(t) for t in tags])


@tags_bp.route("/images/<int:image_id>/tags", methods=["POST"])
async def add_tag_to_image(image_id: int):
    body = await request.get_json()
    if not body or "tag_id" not in body:
        return jsonify({"error": "tag_id required"}), 400
    source = body.get("source", "manual")

    async with get_session() as session:
        svc = TagService(session)
        await svc.add_to_image(image_id, body["tag_id"], source=source)
        await session.commit()
    return "", 204


@tags_bp.route("/images/<int:image_id>/tags/<int:tag_id>", methods=["DELETE"])
async def remove_tag_from_image(image_id: int, tag_id: int):
    async with get_session() as session:
        svc = TagService(session)
        await svc.remove_from_image(image_id, tag_id)
        await session.commit()
    return "", 204


@tags_bp.route("/tags/<int:tag_id>", methods=["GET"])
async def get_tag(tag_id: int):
    """Resolve a single tag (used by the gallery to label its active
    tag-filter chip)."""
    async with get_session() as session:
        tag = await session.get(Tag, tag_id)
        if tag is None:
            return jsonify({"error": "tag not found"}), 404
        return jsonify(
            {
                "id": tag.id,
                "name": tag.name,
                "kind": tag.kind.value,
                "fandom_id": tag.fandom_id,
            }
        )


@tags_bp.route("/tags/<int:tag_id>/aliases", methods=["GET"])
async def list_tag_aliases(tag_id: int):
    """Model keys that fold into this tag (tag-side alias view). Remove via the
    shared DELETE /api/aliases/<string>/<category>."""
    async with get_session() as session:
        if await session.get(Tag, tag_id) is None:
            return jsonify({"error": "tag not found"}), 404
        rows = await AliasService(session).list_for_tag(tag_id)
    return jsonify(
        [
            {
                "alias_string": r.alias_string,
                "alias_category": r.alias_category,
            }
            for r in rows
        ]
    )


@tags_bp.route("/tags/<int:tag_id>", methods=["PATCH"])
async def update_tag(tag_id: int):
    """Rename and/or re-fandom a tag. Body may carry `name` and/or
    `fandom_id` (a fandom tag id, or null to clear — character tags only).
    `merge: true` resolves a collision by merging into the existing tag.
    """
    body = await request.get_json() or {}
    has_name = "name" in body
    has_fandom = "fandom_id" in body
    if not has_name and not has_fandom:
        return jsonify({"error": "name or fandom_id required"}), 400
    do_merge = bool(body.get("merge"))
    async with get_session() as session:
        svc = TagService(session)
        try:
            tag = None
            if has_name:
                tag = await svc.rename(tag_id, body["name"])
            if has_fandom:
                tag = await svc.set_fandom(
                    tag_id, body["fandom_id"], merge=do_merge
                )
        except TagMergeConflict as exc:
            return jsonify(
                {
                    "error": str(exc),
                    "target": {
                        "id": exc.target_id,
                        "name": exc.target_name,
                    },
                    "source_image_count": exc.source_image_count,
                    "will_alias": exc.will_alias,
                }
            ), 409
        except TagValidationError as exc:
            return jsonify({"error": str(exc)}), 400
        await session.commit()
        return jsonify(
            {
                "id": tag.id,
                "name": tag.name,
                "kind": tag.kind.value,
                "fandom_id": tag.fandom_id,
            }
        )


@tags_bp.route("/tags/<int:source_id>/merge", methods=["POST"])
async def merge_tag(source_id: int):
    body = await request.get_json()
    if not body or "target_id" not in body:
        return jsonify({"error": "target_id required"}), 400
    target_id = body["target_id"]
    async with get_session() as session:
        svc = TagService(session)
        try:
            result = await svc.merge(source_id, target_id)
        except TagValidationError as exc:
            msg = str(exc)
            status = 404 if "not found" in msg else 400
            return jsonify({"error": msg}), status
        await session.commit()
        target_allowlisted = await session.scalar(
            select(exists().where(TagAllowlist.tag_id == result.target_id))
        )
    if target_allowlisted:
        from ..tasks.ml import apply_allowlist_tags

        apply_allowlist_tags.delay(tag_id=result.target_id)
    # Tag merge invalidates the target's centroid (the merged-in source
    # tag's images now contribute to it). Daily list_drifted catches it
    # within 24h, but eager recompute closes the suggestion-quality dip
    # in the meantime. Audit 2026-06-02.
    from ..tasks.ml import recompute_centroid
    recompute_centroid.delay(result.target_id)
    return jsonify(
        {
            "target": {
                "id": result.target_id,
                "name": result.target_name,
                "kind": result.target_kind,
            },
            "merged_count": result.merged_count,
            "alias_created": result.alias_created,
            "source_deleted": result.source_deleted,
        }
    )


@tags_bp.route("/images/common-tags", methods=["POST"])
async def common_tags():
    body = await request.get_json()
    ids, err = _parse_bulk_ids(body)
    if err:
        return err
    async with get_session() as session:
        tags = await BulkTagService(session).common_tags(ids)
    return jsonify({"tags": tags})


@tags_bp.route("/images/cluster/tag-gaps", methods=["POST"])
async def cluster_tag_gaps():
    """Cluster-consensus tag gaps for a visual neighbour set (#94 Explore):
    tags on >= threshold of the images but not all. Each gap carries the
    laggard image ids (minus rejections) for apply-to-cluster."""
    body = await request.get_json()
    ids, err = _parse_bulk_ids(body)
    if err:
        return err
    try:
        threshold = float(body.get("threshold", 0.6))
    except (TypeError, ValueError):
        threshold = 0.6
    threshold = min(1.0, max(0.0, threshold))
    async with get_session() as session:
        gaps = await BulkTagService(session).tag_gaps(ids, threshold)
    return jsonify(
        {"gaps": gaps, "total": len(set(ids)), "threshold": threshold}
    )


@tags_bp.route("/images/bulk/tags", methods=["POST"])
async def bulk_add_tag():
    body = await request.get_json()
    ids, err = _parse_bulk_ids(body)
    if err:
        return err
    if not body or "tag_id" not in body:
        return jsonify({"error": "tag_id required"}), 400
    source = body.get("source", "manual")
    if source not in ("manual", "ml_accepted"):
        return jsonify({"error": f"invalid source {source!r}"}), 400
    async with get_session() as session:
        if await session.get(Tag, body["tag_id"]) is None:
            return jsonify({"error": "tag not found"}), 404
        try:
            added = await BulkTagService(session).bulk_add(
                ids, body["tag_id"], source=source
            )
        except IntegrityError:
            return jsonify(
                {"error": "one or more image_ids do not exist"}
            ), 400
        await session.commit()
    return jsonify({"added_count": added, "total": len(ids)})


@tags_bp.route("/images/bulk/tags/remove", methods=["POST"])
async def bulk_remove_tag():
    body = await request.get_json()
    ids, err = _parse_bulk_ids(body)
    if err:
        return err
    if not body or "tag_id" not in body:
        return jsonify({"error": "tag_id required"}), 400
    async with get_session() as session:
        if await session.get(Tag, body["tag_id"]) is None:
            return jsonify({"error": "tag not found"}), 404
        removed = await BulkTagService(session).bulk_remove(
            ids, body["tag_id"]
        )
        await session.commit()
    return jsonify({"removed_count": removed})


def _series_err(exc: SeriesError):
    msg = str(exc)
    status = 404 if ("not found" in msg or "not a series" in msg) else 400
    return jsonify({"error": msg}), status


def _opt_int(body, key: str):
    """(value, error) — value is None when absent, error is (json, status)."""
    if not body or body.get(key) is None:
        return None, None
    try:
        return int(body[key]), None
    except (TypeError, ValueError):
        return None, (jsonify({"error": f"{key} must be an integer"}), 400)


def _parse_int_list(body, key: str, *, max_ids: int = 500):
    """(list, error) for a required list of ints under `key`."""
    if not body or key not in body:
        return None, (jsonify({"error": f"{key} required"}), 400)
    raw = body[key]
    if not isinstance(raw, list) or not raw:
        return None, (jsonify({"error": f"{key} must be a non-empty list"}), 400)
    if len(raw) > max_ids:
        return None, (jsonify({"error": f"too many ids (max {max_ids})"}), 400)
    try:
        return [int(x) for x in raw], None
    except (TypeError, ValueError):
        return None, (jsonify({"error": f"{key} must be integers"}), 400)


@tags_bp.route("/series/<int:tag_id>/pages", methods=["GET"])
async def series_pages(tag_id: int):
    async with get_session() as session:
        try:
            data = await SeriesService(session).list_pages(tag_id)
        except SeriesError as exc:
            return _series_err(exc)
    return jsonify(data)


@tags_bp.route("/series/<int:tag_id>/pages", methods=["POST"])
async def series_add(tag_id: int):
    body = await request.get_json()
    ids, err = _parse_bulk_ids(body, max_ids=500)
    if err:
        return err
    async with get_session() as session:
        try:
            n = await SeriesService(session).add_images(tag_id, ids)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"added_count": n})


@tags_bp.route("/series/<int:tag_id>/pages/remove", methods=["POST"])
async def series_remove(tag_id: int):
    body = await request.get_json()
    ids, err = _parse_bulk_ids(body, max_ids=500)
    if err:
        return err
    async with get_session() as session:
        try:
            n = await SeriesService(session).remove_images(tag_id, ids)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"removed_count": n})


@tags_bp.route("/series/<int:tag_id>/pages/number", methods=["POST"])
async def series_set_page_number(tag_id: int):
    """Set one placed page's number — the operator's value (sparse, gaps
    allowed); pass page_number: null to leave it unnumbered."""
    body = await request.get_json() or {}
    image_id, ierr = _opt_int(body, "image_id")
    if ierr:
        return ierr
    if image_id is None:
        return jsonify({"error": "image_id required"}), 400
    if "page_number" not in body:
        return jsonify({"error": "page_number required (may be null)"}), 400
    page_number, perr = _opt_int(body, "page_number")
    if perr:
        return perr
    async with get_session() as session:
        try:
            await SeriesService(session).set_page_number(
                tag_id, image_id, page_number
            )
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"ok": True})


@tags_bp.route("/series/<int:tag_id>/cover", methods=["POST"])
async def series_cover(tag_id: int):
    body = await request.get_json()
    if not body or "image_id" not in body:
        return jsonify({"error": "image_id required"}), 400
    try:
        image_id = int(body["image_id"])
    except (TypeError, ValueError):
        return jsonify({"error": "image_id must be an integer"}), 400
    async with get_session() as session:
        try:
            await SeriesService(session).set_cover(tag_id, image_id)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"ok": True})


# ---- chapter dividers (FC-6.x) -------------------------------------------
# A chapter is a cosmetic divider anchored to the page that begins it; it owns
# no pages. Page ordering follows each page's operator-set number (the
# /pages/number endpoint), so there is no per-chapter reorder/merge — those are
# gone.


@tags_bp.route("/series/<int:tag_id>/chapters", methods=["POST"])
async def series_chapter_create(tag_id: int):
    body = await request.get_json() or {}
    anchor, aerr = _opt_int(body, "anchor_image_id")
    if aerr:
        return aerr
    if anchor is None:
        return jsonify({"error": "anchor_image_id required"}), 400
    title = body.get("title")
    if title is not None and not isinstance(title, str):
        return jsonify({"error": "title must be a string"}), 400
    part, perr = _opt_int(body, "stated_part")
    if perr:
        return perr
    async with get_session() as session:
        try:
            ch = await SeriesService(session).create_divider(
                tag_id, anchor, title=title, stated_part=part,
            )
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify(ch)


@tags_bp.route(
    "/series/<int:tag_id>/chapters/<int:chapter_id>", methods=["PATCH"]
)
async def series_chapter_update(tag_id: int, chapter_id: int):
    body = await request.get_json() or {}
    kwargs: dict = {}
    if "title" in body:
        if body["title"] is not None and not isinstance(body["title"], str):
            return jsonify({"error": "title must be a string"}), 400
        kwargs.update(set_title=True, title=body["title"])
    if "stated_part" in body:
        part, perr = _opt_int(body, "stated_part")
        if perr:
            return perr
        kwargs.update(set_part=True, stated_part=part)
    if "anchor_image_id" in body:
        anchor, aerr = _opt_int(body, "anchor_image_id")
        if aerr:
            return aerr
        if anchor is None:
            return jsonify(
                {"error": "anchor_image_id must be an integer"}
            ), 400
        kwargs.update(set_anchor=True, anchor_image_id=anchor)
    async with get_session() as session:
        try:
            await SeriesService(session).update_divider(
                tag_id, chapter_id, **kwargs
            )
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"ok": True})


@tags_bp.route(
    "/series/<int:tag_id>/chapters/<int:chapter_id>", methods=["DELETE"]
)
async def series_chapter_delete(tag_id: int, chapter_id: int):
    async with get_session() as session:
        try:
            await SeriesService(session).delete_divider(tag_id, chapter_id)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"ok": True})


# ---- browse list + post→series flows (FC-6.2) -----------------------------


@tags_bp.route("/series", methods=["GET"])
async def series_list():
    args = request.args
    sort = args.get("sort", "recent")
    if sort not in ("recent", "name", "size"):
        return jsonify({"error": "sort must be recent|name|size"}), 400
    artist_id = None
    if args.get("artist_id") is not None:
        try:
            artist_id = int(args["artist_id"])
        except ValueError:
            return jsonify({"error": "artist_id must be an integer"}), 400
    async with get_session() as session:
        rows = await SeriesService(session).list_series(
            sort=sort, artist_id=artist_id
        )
    return jsonify({"series": rows})


@tags_bp.route("/series/from-post", methods=["POST"])
async def series_from_post():
    body = await request.get_json()
    post_id, err = _opt_int(body, "post_id")
    if err:
        return err
    if post_id is None:
        return jsonify({"error": "post_id required"}), 400
    async with get_session() as session:
        try:
            out = await SeriesService(session).promote_post_to_series(post_id)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify(out)


@tags_bp.route("/series/<int:tag_id>/add-post", methods=["POST"])
async def series_add_post(tag_id: int):
    body = await request.get_json()
    post_id, err = _opt_int(body, "post_id")
    if err:
        return err
    if post_id is None:
        return jsonify({"error": "post_id required"}), 400
    async with get_session() as session:
        try:
            out = await SeriesService(session).add_post(tag_id, post_id)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify(out)


@tags_bp.route("/series/<int:tag_id>/pending/place", methods=["POST"])
async def series_place_pending(tag_id: int):
    """Place staged (pending) pages into the run, numbered sequentially from
    `start_page` in the given order (#789). start_page null → unnumbered."""
    body = await request.get_json()
    ids, err = _parse_bulk_ids(body, max_ids=500)
    if err:
        return err
    start, serr = _opt_int(body, "start_page")
    if serr:
        return serr
    async with get_session() as session:
        try:
            n = await SeriesService(session).place_pending(
                tag_id, ids, start_page=start
            )
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"placed_count": n})


# ---- suggestion queue (FC-6.3) --------------------------------------------


@tags_bp.route("/series/suggestions", methods=["GET"])
async def series_suggestions_list():
    async with get_session() as session:
        rows = await SeriesMatchService(session).list_pending()
    return jsonify({"suggestions": rows})


@tags_bp.route("/series/suggestions/<int:sid>/accept", methods=["POST"])
async def series_suggestion_accept(sid: int):
    async with get_session() as session:
        try:
            out = await SeriesMatchService(session).accept(sid)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify(out)


@tags_bp.route("/series/suggestions/<int:sid>/dismiss", methods=["POST"])
async def series_suggestion_dismiss(sid: int):
    async with get_session() as session:
        try:
            await SeriesMatchService(session).dismiss(sid)
        except SeriesError as exc:
            return _series_err(exc)
        await session.commit()
    return jsonify({"ok": True})


@tags_bp.route("/series/suggestions/rescan", methods=["POST"])
async def series_suggestions_rescan():
    from ..tasks.admin import rescan_series_suggestions_task

    res = rescan_series_suggestions_task.delay()
    return jsonify({"task_id": res.id})
