#!/usr/bin/env python3
"""Convert box test images to searchable PDFs via natural-pdf + rapidocr."""

import sys
from pathlib import Path

# Add project root to path
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

from natural_pdf import PDF

BASE_DIR = Path(__file__).parent

IMAGES = [
    ("boxes-1", BASE_DIR / "boxes-1.png"),
    ("boxes-2", BASE_DIR / "boxes-2.png"),
]

for name, img_path in IMAGES:
    if not img_path.exists():
        print(f"Missing: {img_path}")
        continue

    print(f"\nProcessing {name}...")
    pdf = PDF.from_images(str(img_path))
    page = pdf.pages[0]

    print(f"  Page size: {page.width}x{page.height}")
    print(f"  Applying OCR (rapidocr)...")
    page.apply_ocr(engine="rapidocr")

    ocr_elements = page.find_all("text[source=ocr]")
    print(f"  OCR found {len(ocr_elements)} text elements")

    out_path = BASE_DIR / f"{name}.pdf"
    print(f"  Saving searchable PDF: {out_path}")
    pdf.save_pdf(str(out_path), ocr=True)
    pdf.close()
    print(f"  Done: {out_path}")
