"""Create Filepetal's synthetic PDF fixtures. Requires pypdf; no personal data."""
import argparse
import hashlib
import json
from pathlib import Path
from pypdf import PdfWriter
from pypdf.generic import DictionaryObject, NameObject, DecodedStreamObject


def make_pdf(path, pages):
    writer = PdfWriter()
    font = writer._add_object(DictionaryObject({
        NameObject('/Type'): NameObject('/Font'),
        NameObject('/Subtype'): NameObject('/Type1'),
        NameObject('/BaseFont'): NameObject('/Helvetica'),
    }))
    for number, (width, height, rotation) in enumerate(pages, 1):
        page = writer.add_blank_page(width=width, height=height)
        page[NameObject('/Resources')] = DictionaryObject({
            NameObject('/Font'): DictionaryObject({NameObject('/F1'): font})
        })
        marker = f'PAGE {number} - TOP LEFT - {width:g} x {height:g} pt'
        stream = DecodedStreamObject()
        stream.set_data((
            f'0.15 0.45 0.6 rg 18 {height - 80:g} 150 50 re f\n'
            f'0 0 0 rg BT /F1 12 Tf 18 {height - 20:g} Td ({marker}) Tj ET\n'
            f'0.9 0.3 0.2 rg {width - 38:g} 18 20 20 re f\n'
        ).encode('ascii'))
        page[NameObject('/Contents')] = writer._add_object(stream)
        if rotation:
            page.rotate(rotation)
    writer.add_metadata({'/Title': 'Filepetal synthetic PDF check', '/Subject': 'CC0 geometric fixtures; 2026-10-02'})
    with path.open('wb') as output:
        writer.write(output)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--output', default='pdf-jpg-samples')
    args = parser.parse_args()
    target = Path(args.output)
    target.mkdir(parents=True, exist_ok=True)
    cases = {
        'letter-marker': [(612, 792, 0)],
        'mixed-pages': [(612, 792, 0), (595.275590551, 841.88976378, 0), (612, 792, 90)],
        'wide-page': [(10000, 100, 0)],
        'twenty-one-pages': [(72, 72, 0)] * 21,
        'combined-limit': [(1300, 1300, 0)] * 6,
    }
    manifest = {'checked_date': '2026-10-02', 'license': 'CC0-1.0', 'synthetic': True, 'cases': []}
    for name, pages in cases.items():
        path = target / f'{name}.pdf'
        make_pdf(path, pages)
        data = path.read_bytes()
        manifest['cases'].append({'file': path.name, 'bytes': len(data), 'sha256': hashlib.sha256(data).hexdigest(),
                                  'pages': [{'width_pt': w, 'height_pt': h, 'rotation_degrees': r} for w, h, r in pages]})
    (target / 'sample-manifest.json').write_text(json.dumps(manifest, indent=2) + '\n', encoding='utf-8')
    (target / 'README.txt').write_text('Filepetal synthetic PDF fixtures, checked 2026-10-02.\nCC0-1.0: geometric graphics and markers made by Filepetal. No personal documents.\nRun create_samples.py with pypdf to recreate inputs. Object IDs, library versions and metadata can change file hashes.\nBrowser-generated outputs and hashes are separate recorded checks, not generated by this script.\n', encoding='utf-8')
    print(json.dumps(manifest, indent=2))


if __name__ == '__main__':
    main()
