"""CCIP/region observability API (#114) — coverage overview + per-image detail."""
import pytest

from backend.app.models import ImageRecord, ImageRegion, TagKind
from backend.app.models.tag import image_tag
from backend.app.services.tag_service import TagService

pytestmark = pytest.mark.integration


def _ccip(slot: int) -> list[float]:
    v = [0.0] * 768
    v[slot] = 1.0
    return v


async def _img(db, sha) -> ImageRecord:
    img = ImageRecord(
        path=f"/images/{sha}.jpg", sha256=sha, size_bytes=1, mime="image/jpeg",
        width=1, height=1, origin="imported_filesystem", integrity_status="unknown",
    )
    db.add(img)
    await db.flush()
    return img


async def _figure(db, image_id, ccip):
    db.add(ImageRegion(
        image_record_id=image_id, kind="figure", rx=0.0, ry=0.0, rw=1.0, rh=1.0,
        ccip_embedding=ccip, embedding_version="ccip-test",
    ))


@pytest.mark.asyncio
async def test_overview_reports_coverage(client, db):
    raven = await TagService(db).find_or_create("Raven", TagKind.character)
    ref = await _img(db, "a" * 64)
    await _figure(db, ref.id, _ccip(0))
    await db.execute(image_tag.insert().values(
        image_record_id=ref.id, tag_id=raven.id, source="manual",
    ))
    q = await _img(db, "b" * 64)
    await _figure(db, q.id, _ccip(0))
    await db.commit()

    body = await (await client.get("/api/ccip/overview")).get_json()
    assert body["regions_by_kind"].get("figure", 0) >= 2
    assert body["images_with_figure_ccip"] >= 2
    assert any(
        c["name"] == "Raven" and c["n_refs"] >= 1
        for c in body["character_references"]
    )
    assert "ccip-test" in body["embedding_versions"]


@pytest.mark.asyncio
async def test_image_detail_shows_regions_and_matches(client, db):
    raven = await TagService(db).find_or_create("Raven", TagKind.character)
    ref = await _img(db, "c" * 64)
    await _figure(db, ref.id, _ccip(0))
    await db.execute(image_tag.insert().values(
        image_record_id=ref.id, tag_id=raven.id, source="manual",
    ))
    q = await _img(db, "d" * 64)
    await _figure(db, q.id, _ccip(0))
    await db.commit()

    body = await (await client.get(f"/api/ccip/images/{q.id}")).get_json()
    assert len(body["regions"]) == 1
    r = body["regions"][0]
    assert r["kind"] == "figure" and r["has_ccip"] is True and r["has_siglip"] is False
    assert any(m["tag_id"] == raven.id for m in body["ccip_matches"])
