"""Verify recorded browser outputs, not simulated converter results."""
import hashlib
import json
from io import BytesIO
from pathlib import Path
import zipfile
from PIL import Image

root = Path(__file__).resolve().parent
results = {'checked_date': '2026-10-02', 'source': 'Actual local browser conversion outputs', 'files': []}


def jpeg_check(name, data, expected):
    with Image.open(BytesIO(data)) as image:
        image.load()
        assert image.format == 'JPEG'
        assert image.size == expected
        assert min(image.convert('RGB').getpixel((5, 5))) > 245
        return {'file': name, 'bytes': len(data), 'sha256': hashlib.sha256(data).hexdigest(),
                'format': image.format, 'width': image.width, 'height': image.height,
                'white_corner_verified': True, 'exif_tags': list(image.getexif().keys())}


for name, dimensions in [('letter-marker.jpg', (1224, 1584)), ('wide-page.jpg', (4096, 41))]:
    results['files'].append(jpeg_check(name, (root / name).read_bytes(), dimensions))

archive = (root / 'mixed-pages.zip').read_bytes()
zip_result = {'file': 'mixed-pages.zip', 'bytes': len(archive), 'sha256': hashlib.sha256(archive).hexdigest(), 'pages': []}
with zipfile.ZipFile(BytesIO(archive)) as z:
    names = z.namelist()
    assert names == ['page-01.jpg', 'page-02.jpg', 'page-03.jpg']
    for name, dimensions in zip(names, [(1224, 1584), (1191, 1684), (1584, 1224)]):
        data = z.read(name)
        output = 'mixed-' + name
        (root / output).write_bytes(data)
        zip_result['pages'].append(jpeg_check(output, data, dimensions))
    with Image.open(BytesIO(z.read('page-03.jpg'))) as image:
        pixel = image.convert('RGB').getpixel((1463, 100))
        assert pixel[2] > pixel[0] + 50 and pixel[1] > pixel[0] + 30
        zip_result['rotation_marker_verified'] = True
results['files'].append(zip_result)
(root / 'output-verification.json').write_text(json.dumps(results, indent=2) + '\n', encoding='utf-8')
print(json.dumps(results, indent=2))
