#!/usr/bin/env python3
"""Render onboarding favicon.svg → PNG/ICO using PIL (matching SVG exactly)."""
from PIL import Image, ImageDraw
import struct, io, os

COLOR = (242, 107, 107)   # #F26B6B

def draw_mark(draw, size):
    """SVG viewBox 0 0 100 100, transparent bg, pink EN arrows."""
    def pt(x, y):
        return (x / 100 * size, y / 100 * size)

    # Right wing: M50 12 L84 88 L50 72 Z — solid fill
    right = [pt(50, 12), pt(84, 88), pt(50, 72)]
    draw.polygon(right, fill=COLOR)

    # Left wing: M50 12 L16 88 L50 72 — stroke only, no fill
    stroke_w = max(2, int(size * 0.053))   # 5/100 * size
    left_pts = [pt(50, 12), pt(16, 88), pt(50, 72)]
    draw.line(left_pts, fill=COLOR, width=stroke_w, joint="curve")

def make_icon(size, bg=None):
    """bg=None → transparent; bg=(r,g,b) → solid background."""
    if bg:
        img = Image.new("RGBA", (size, size), bg + (255,))
    else:
        img = Image.new("RGBA", (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    draw_mark(draw, size)
    return img

OUT = "/Users/bryce/FLSM/.tmp/onboarding_favicon_out"
os.makedirs(OUT, exist_ok=True)

sizes = [16, 32, 48, 64, 128, 192, 256, 512]
icons = {s: make_icon(s) for s in sizes}

# Save individual PNGs
for s, img in icons.items():
    img.save(os.path.join(OUT, f"icon_{s}.png"))

# icon.png (192) — transparent
icons[192].save(os.path.join(OUT, "icon.png"))

# apple-touch-icon (180) — white background (iOS requires solid)
make_icon(180, bg=(255,255,255)).save(os.path.join(OUT, "apple-touch-icon.png"))

# favicon.ico (16, 32, 48, 256)
ico_sizes = [16, 32, 48, 256]
ico_images = []
for s in ico_sizes:
    buf = io.BytesIO()
    icons[s].save(buf, format="PNG")
    ico_images.append((s, buf.getvalue()))

num = len(ico_images)
header = struct.pack("<HHH", 0, 1, num)
offset = 6 + num * 16
offsets = []
for s, data in ico_images:
    offsets.append(offset)
    offset += len(data)
directory = b""
for i, (s, data) in enumerate(ico_images):
    w = 0 if s == 256 else s
    h = 0 if s == 256 else s
    directory += struct.pack("<BBBBHHII", w, h, 0, 0, 1, 32, len(data), offsets[i])
ico_bytes = header + directory + b"".join(d for _, d in ico_images)
with open(os.path.join(OUT, "favicon.ico"), "wb") as f:
    f.write(ico_bytes)

print("Generated:")
for fn in sorted(os.listdir(OUT)):
    print(f"  {fn}  ({os.path.getsize(os.path.join(OUT, fn))} bytes)")
