#!/usr/bin/env python3
"""Backfill FDI structured index for already-ingested ready PDFs."""

from __future__ import annotations

import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from app.config import get_settings
from app.database import get_connection
from app.fdi.pipeline import index_document_structured
from app.fdi.schema import init_fdi_tables
from app.saas.kb import org_storage_dir


def main() -> int:
    get_settings.cache_clear()
    init_fdi_tables()
    with get_connection() as conn:
        rows = conn.execute(
            """
            SELECT id, org_id, agent_id, filename
            FROM kb_documents
            WHERE status = 'ready' AND lower(filename) LIKE '%.pdf'
            ORDER BY updated_at DESC
            """
        ).fetchall()

    ok = fail = 0
    for r in rows:
        d = dict(r)
        org_id, agent_id, doc_id = d["org_id"], d["agent_id"], d["id"]
        filename = d["filename"]
        folder = org_storage_dir(org_id, agent_id)
        matches = list(folder.glob(f"{doc_id}_*"))
        if not matches:
            print(f"SKIP missing file {filename} ({doc_id})")
            fail += 1
            continue
        path = matches[0]
        try:
            report = index_document_structured(
                org_id=org_id,
                agent_id=agent_id,
                kb_doc_id=doc_id,
                path=path,
                filename=filename,
                mime_type="application/pdf",
            )
            print(
                f"OK {filename} type={report['doc_type']} "
                f"lines={report['line_count']} ids={report['identifier_count']}"
            )
            ok += 1
        except Exception as exc:
            print(f"FAIL {filename}: {exc}")
            fail += 1
    print(f"\nDone. ok={ok} fail={fail}")
    return 0 if fail == 0 else 1


if __name__ == "__main__":
    raise SystemExit(main())
