"""Create a controlled logo specimen, not a recovered or downloaded brand asset."""
from pathlib import Path
from PIL import Image, ImageDraw, __version__ as pillow_version
import hashlib
import json
import platform

ROOT = Path(__file__).resolve().parent
OUT = ROOT
OUT.mkdir(parents=True, exist_ok=True)
scale = 4
im = Image.new('RGBA', (320 * scale, 200 * scale), (0, 0, 0, 0))
d = ImageDraw.Draw(im)
def box(coords):
    return tuple(int(v * scale) for v in coords)
d.ellipse(box((36, 36, 164, 164)), fill='#157F7C')
d.rectangle(box((180, 60, 284, 86)), fill='#182A3B')
d.rectangle(box((180, 100, 254, 126)), fill='#182A3B')
d.ellipse(box((258, 130, 286, 158)), fill='#EE6C4D')
logo = im.resize((320, 200), Image.Resampling.LANCZOS)
white = Image.new('RGBA', logo.size, 'white')
white.alpha_composite(logo)
fixtures = [('logo-transparent', logo), ('logo-white', white)]
# An edge picked by an explicit rule, not by guessing its pixel value.
edge = next((x, y) for y in range(50, 101) for x in range(30, 101)
            if 32 <= logo.getpixel((x, y))[3] <= 223)
coords = [('teal interior', 100, 100), ('ink interior', 210, 70),
          ('coral interior', 272, 144), ('teal antialiased edge', *edge),
          ('background', 10, 10)]
records = []
for name, image in fixtures:
    path = OUT / (name + '.png')
    image.save(path)
    raw_path = OUT / (name + '.rgba')
    raw_path.write_bytes(image.tobytes())
    records.append({
        'name': name, 'file': path.name, 'rgba_file': raw_path.name,
        'width': image.width, 'height': image.height,
        'sha256': hashlib.sha256(path.read_bytes()).hexdigest(),
        'rgba_sha256': hashlib.sha256(raw_path.read_bytes()).hexdigest(),
        'samples': [{'label': label, 'x': x, 'y': y,
                     'rgba': list(image.getpixel((x, y))),
                     'hex_rgb': '#' + ''.join(f'{v:02X}' for v in image.getpixel((x, y))[:3])}
                    for label, x, y in coords],
    })
report = {
    'status': 'local controlled fixture; browser upload and CSS download not yet checked',
    'python': platform.python_version(), 'pillow': pillow_version,
    'construction': 'Original geometric specimen drawn at 4x and LANCZOS reduced to 320x200. Same RGBA logo flattened onto white for the second specimen. No ICC profile.',
    'coordinate_convention': 'zero-based x/y from top left',
    'edge_selection': 'first pixel in row-major order in x=[30,100], y=[50,100] with alpha in [32,223]',
    'authored_fills': {'teal': '#157F7C', 'ink': '#182A3B', 'coral': '#EE6C4D'},
    'fixtures': records,
}
(OUT / 'fixture-results.json').write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
print(json.dumps(report, indent=2))
