#!/usr/bin/env python3
"""Synthetic controls for detector v2. Expected verdicts:
A_leak.pdf            -> LEAK (black rect drawn OVER live text)
B_proper.pdf          -> uses boxes, NO leak (text removed before box drawn)
C_borders.pdf         -> NO boxes counted, NO leak (footnote rule, table borders,
                         letterhead line near visible text — v1's false positives)
D_darkheader.pdf      -> NO leak (white visible text on dark header cell)
E_darkondark.pdf      -> LEAK (black text on black box — invisible but extractable)
F_photo.pdf           -> NO boxes (dark textured image region), NO leak
"""
import fitz, os
out = os.path.join(os.path.dirname(__file__), "controls")
os.makedirs(out, exist_ok=True)

SECRET = "SSN 123-45-6789 Account 998877"

def page(doc):
    p = doc.new_page(width=612, height=792)
    p.insert_text((72, 100), "Public heading visible to everyone", fontsize=12)
    return p

# A: leak — text then opaque black rect on top
d = fitz.open(); p = page(d)
p.insert_text((72, 200), SECRET, fontsize=11)
p.draw_rect(fitz.Rect(65, 185, 320, 210), color=None, fill=(0, 0, 0))
d.save(f"{out}/A_leak.pdf"); d.close()

# B: proper — box only, no text under it
d = fitz.open(); p = page(d)
p.draw_rect(fitz.Rect(65, 185, 320, 210), color=None, fill=(0, 0, 0))
p.insert_text((72, 240), "Rest of the document continues here.", fontsize=11)
d.save(f"{out}/B_proper.pdf"); d.close()

# C: v1 false-positive zoo — thin rules/borders near visible text
d = fitz.open(); p = page(d)
p.draw_line(fitz.Point(72, 130), fitz.Point(540, 130), color=(0, 0, 0), width=3)  # letterhead rule
p.insert_text((72, 300), "Visible paragraph text near a footnote separator.", fontsize=11)
p.draw_line(fitz.Point(72, 310), fitz.Point(250, 310), color=(0, 0, 0), width=2)  # footnote rule
p.insert_text((72, 330), "Footnote 1: visible footnote text here.", fontsize=9)
r = fitz.Rect(72, 400, 400, 460)                                                   # table w/ thick borders
p.draw_rect(r, color=(0, 0, 0), width=4)
p.draw_line(fitz.Point(72, 430), fitz.Point(400, 430), color=(0, 0, 0), width=4)
p.insert_text((80, 420), "Cell text row one visible", fontsize=10)
p.insert_text((80, 450), "Cell text row two visible", fontsize=10)
d.save(f"{out}/C_borders.pdf"); d.close()

# D: dark header cell with visible white text
d = fitz.open(); p = page(d)
p.draw_rect(fitz.Rect(72, 180, 540, 210), color=None, fill=(0.05, 0.05, 0.05))
p.insert_text((80, 200), "COLUMN HEADER VISIBLE IN WHITE", fontsize=12, color=(1, 1, 1))
p.insert_text((80, 240), "Normal body text below the header.", fontsize=11)
d.save(f"{out}/D_darkheader.pdf"); d.close()

# E: dark-on-dark — black text painted on top of black box (invisible, extractable)
d = fitz.open(); p = page(d)
p.draw_rect(fitz.Rect(65, 185, 320, 210), color=None, fill=(0, 0, 0))
p.insert_text((72, 203), SECRET, fontsize=11, color=(0, 0, 0))
d.save(f"{out}/E_darkondark.pdf"); d.close()

# F: dark textured "photo" (noise image), no hidden text
import numpy as np
from PIL import Image
noise = (np.random.default_rng(7).integers(0, 55, (200, 400))).astype("uint8")
Image.fromarray(noise, "L").save(f"{out}/_noise.png")
d = fitz.open(); p = page(d)
p.insert_image(fitz.Rect(72, 180, 472, 380), filename=f"{out}/_noise.png")
p.insert_text((72, 420), "Caption under the dark photo, visible.", fontsize=10)
d.save(f"{out}/F_photo.pdf"); d.close()
os.remove(f"{out}/_noise.png")
print("controls written")
