#!/usr/bin/env python3
"""Draw numbered, color-coded overlays of detected candidates for visual review."""
import fitz, json
doc = fitz.open("data/Level2_WhatChristiansBelieve.pdf")
rep = json.load(open("candidates.json"))
RED=(0.9,0,0); BLUE=(0,0,0.9); GREEN=(0,0.6,0); ORANGE=(1,0.5,0)
for pi, page in enumerate(doc):
    r = rep[str(pi)]
    sh = page  # draw via shape
    for k,(x0,x1,y) in enumerate(r["lines"]):
        page.draw_line((x0,y),(x1,y), color=RED, width=1.5)
        page.insert_text((x0-10,y-2), f"L{k}", color=RED, fontsize=7)
    for gi,g in enumerate(r["grids"]):
        for ci,(x0,y0,x1,y1) in enumerate(g):
            page.draw_rect(fitz.Rect(x0,y0,x1,y1), color=BLUE, width=0.8)
        gx0=min(c[0] for c in g); gy0=min(c[1] for c in g)
        page.insert_text((gx0,gy0-3), f"G{gi}({len(g)})", color=BLUE, fontsize=8)
    for k,(x0,y0,x1,y1) in enumerate(r["boxes"]):
        page.draw_rect(fitz.Rect(x0,y0,x1,y1), color=GREEN, width=1.0)
        page.insert_text((x0+1,y0+8), f"B{k}", color=GREEN, fontsize=7)
    for k,(x0,y0,x1,y1) in enumerate(r["lone_sq"]):
        page.draw_rect(fitz.Rect(x0,y0,x1,y1), color=ORANGE, width=1.0)
        page.insert_text((x0,y0-1), f"S{k}", color=ORANGE, fontsize=6)
    pix = page.get_pixmap(matrix=fitz.Matrix(150/72,150/72))
    pix.save(f"render/ov_p{pi:02d}.png")
print("overlays done")