"""One-shot: run pending metadata + halacha extraction on the 2 בל"מ
decisions uploaded today (8126/24 + 8047/23). Bypasses MCP because the
running MCP server has stale code; calls the services directly with the
updated local copy.

Run from /home/chaim/legal-ai with the venv:
    POSTGRES_URL=... .venv/bin/python scripts/process_pending_blam.py
"""

import asyncio
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "mcp-server", "src"))

from legal_mcp.services import db
from legal_mcp.services import precedent_library


async def main():
    # Queue metadata extraction too (ingest_internal_decision only queues
    # halacha; metadata fills headnote/summary/key_quote and now also
    # confirms proceeding_type via the new prompt field).
    pool = await db.get_pool()
    async with pool.acquire() as conn:
        rows = await conn.fetch(
            "SELECT id, case_number FROM case_law "
            "WHERE case_number IN ('8126/24','8047/23') "
            "  AND source_kind = 'internal_committee'"
        )
        for r in rows:
            await conn.execute(
                "UPDATE case_law SET metadata_extraction_requested_at = NOW() "
                "WHERE id = $1",
                r["id"],
            )
            print(f"queued metadata for {r['case_number']} ({r['id']})")

    print("\n→ running metadata extraction…")
    meta_result = await precedent_library.process_pending_extractions(
        kind="metadata", limit=10,
    )
    print(meta_result)

    print("\n→ running halacha extraction…")
    halacha_result = await precedent_library.process_pending_extractions(
        kind="halacha", limit=10,
    )
    print(halacha_result)


if __name__ == "__main__":
    asyncio.run(main())
