"""Replay labeled keyword searches before connecting an LLM."""
import sqlite3

documents = [
    ("reset", "Reset your password from the sign-in screen."),
    ("export", "ERR-812 means the export job expired. Create a fresh export job."),
    ("invite", "Invite a teammate from workspace settings."),
    ("invoice", "Download an invoice from billing settings."),
]
cases = [
    ("ERR-812", '"ERR-812"', '"ERR-812"', "export"),
    ("Forgot my login secret", '"login secret"', 'password OR "sign-in"', "reset"),
    ("Get a receipt", 'receipt', 'invoice OR receipt', "invoice"),
]

with sqlite3.connect(":memory:") as db:
    db.execute("CREATE VIRTUAL TABLE docs USING fts5(id UNINDEXED, body)")
    db.executemany("INSERT INTO docs VALUES (?, ?)", documents)
    direct_hits = mapped_hits = 0
    for question, direct, mapped, expected in cases:
        def first_hit(expression):
            row = db.execute(
                "SELECT id FROM docs WHERE docs MATCH ? ORDER BY rank, id LIMIT 1",
                (expression,),
            ).fetchone()
            return row[0] if row else None

        raw, rewritten = first_hit(direct), first_hit(mapped)
        direct_hits += raw == expected
        mapped_hits += rewritten == expected
        print(f"{question}: expected={expected}, direct={raw}, mapped={rewritten}")
    print(f"Top-1: direct {direct_hits}/{len(cases)}, mapped {mapped_hits}/{len(cases)}")
