"""Synthetic fixtures for the 30 September 2026 browser privacy checks.

Requires PyMuPDF and Pillow. Run from the repository root:
  python client/public/benchmarks/create-privacy-fixtures.py
No real customer data, identity numbers, photos, or coordinates are used.
"""
from pathlib import Path
import hashlib, json
import fitz
from PIL import Image, ImageDraw

out = Path(__file__).resolve().parent / '2026-09-30'
out.mkdir(exist_ok=True)

def save_doc(document, name):
    document.set_metadata({'title': 'Synthetic OpenToolSuite test fixture', 'author': 'OpenToolSuite', 'creationDate': 'D:20260930000000Z', 'modDate': 'D:20260930000000Z'})
    document.save(out / name, deflate=True, no_new_id=True)

scan = fitz.open()
for index in range(4):
    vector = fitz.open()
    p = vector.new_page(width=595, height=842)
    p.draw_rect(fitz.Rect(35,35,560,105), color=None, fill=(.08,.18,.23))
    p.insert_text((52,70), f'SYNTHETIC REPORT / PAGE {index+1}', fontsize=18, color=(1,1,1))
    p.insert_text((40,136), 'Fictional fixture. No personal or financial records.', fontsize=12)
    for row in range(18):
        y=176+row*25
        if row%2==0: p.draw_rect(fitz.Rect(38,y-17,557,y+7),color=None,fill=(.94,.96,.96))
        p.insert_text((45,y), f'Row {row+1:02d} / SAMPLE-{index+1}-{row+1:02d}', fontsize=11)
        p.insert_text((370,y), f'{(row+1)*137:,.2f} / checked', fontsize=10)
    p.insert_text((40,675), 'Small-print check: INV-2041 / total 11,800.00 / 8 pt.', fontsize=8)
    p.insert_text((40,790), f'Image-only source. Page {index+1} of 4.', fontsize=9)
    pix=p.get_pixmap(matrix=fitz.Matrix(1050/595,1587/842),alpha=False)
    page=scan.new_page(width=595,height=842)
    page.insert_image(page.rect,pixmap=pix)
save_doc(scan,'synthetic-scan-four-pages.pdf')

for name,label in [('merge-a.pdf','ALPHA / FIRST PAGE'),('merge-b.pdf','BETA / SECOND PAGE')]:
    doc=fitz.open();p=doc.new_page(width=595,height=842)
    p.insert_text((56,75),label,fontsize=20)
    p.insert_text((56,115),'Synthetic merge fixture. Verify this page order.',fontsize=11)
    save_doc(doc,name)

text=fitz.open();p=text.new_page(width=595,height=842)
p.insert_text((56,75),'Selectable text fixture / ABC-2041',fontsize=18)
p.insert_text((56,120),'The compact path should preserve searchable text.',fontsize=11)
save_doc(text,'selectable-text.pdf')

# Only synthetic coordinates, camera label, and comment. Orientation 1 avoids
# confusing metadata removal with a rotation/appearance test.
image=Image.new('RGB',(640,480),(20,51,62));draw=ImageDraw.Draw(image)
draw.rectangle((45,45,595,435),fill=(222,238,231))
draw.text((65,85),'SYNTHETIC PHOTO / NOT A REAL PERSON',fill=(20,51,62))
draw.text((65,130),'No visible identity information.',fill=(20,51,62))
exif=Image.Exif();exif[271]='Synthetic Camera';exif[272]='TEST-2041';exif[274]=1
exif[36867]='2026:09:30 00:00:00';exif[37510]=b'ASCII\0\0\0SYNTHETIC-METADATA-MARKER'
exif[34853]={1:'N',2:(1.0,2.0,3.0),3:'E',4:(4.0,5.0,6.0)}
image.save(out/'synthetic-exif-gps.jpg','JPEG',quality=88,exif=exif)

manifest=[]
input_names=['merge-a.pdf','merge-b.pdf','selectable-text.pdf','synthetic-exif-gps.jpg','synthetic-scan-four-pages.pdf']
for path in [out/name for name in input_names]:
    data=path.read_bytes()
    item={'name':path.name,'bytes':len(data),'sha256':hashlib.sha256(data).hexdigest()}
    if path.suffix=='.pdf':
        doc=fitz.open(path);item.update(pages=len(doc),textCharacters=sum(len(p.get_text()) for p in doc))
    else:
        im=Image.open(path);tags=im.getexif();item.update(width=im.width,height=im.height,exifTags=len(tags),gpsPresent=34853 in tags)
    manifest.append(item)
(out/'fixtures.json').write_text(json.dumps({'generator':{'PyMuPDF':fitz.VersionBind,'Pillow':Image.__version__},'fixtures':manifest},indent=2)+'\n')
print(json.dumps(manifest,indent=2))
