"""
Regenerate src-tauri/icons/source.png — dark rounded square + 2×2 tile grid
with one tile in the active-blue and one in the broadcast-orange. Matches
the running app's visual identity (the colors used for .leaf.active and
.leaf.broadcasting borders).

Run from the project root:
    python3 scripts/make-icon.py
    pnpm tauri icon src-tauri/icons/source.png   # generates ico/icns/etc
"""

import sys
from pathlib import Path

try:
    from PIL import Image, ImageDraw
except ImportError:
    sys.stderr.write("Pillow not installed: pip install --user Pillow\n")
    sys.exit(2)

SIZE = 1024

# Background: app's dark theme, slightly tinted blue
BG = (12, 14, 20, 255)
BG_RADIUS = 180
BG_PAD = 40

# Inner 2x2 grid
TILE_PAD = 170           # distance from outer edge to first tile
TILE_GAP = 36            # gap between tiles
TILE_RADIUS = 44         # rounded corner of each tile

# Tile colors — match tiletopia's UI accents
TILE_MUTED = (44, 48, 58, 255)        # quiet tile (dark gray-blue)
TILE_ACTIVE = (90, 140, 216, 255)     # .leaf.active blue
TILE_BCAST = (224, 152, 56, 255)      # .leaf.broadcasting orange


def main(out: Path) -> None:
    img = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)

    # Outer rounded square (the app body)
    draw.rounded_rectangle(
        [BG_PAD, BG_PAD, SIZE - BG_PAD, SIZE - BG_PAD],
        radius=BG_RADIUS,
        fill=BG,
    )

    # 2x2 tile grid inside
    inner_left = TILE_PAD
    inner_top = TILE_PAD
    inner_right = SIZE - TILE_PAD
    inner_bottom = SIZE - TILE_PAD
    tile_size = ((inner_right - inner_left) - TILE_GAP) // 2

    positions = [
        (inner_left, inner_top, TILE_ACTIVE),                              # TL: active
        (inner_left + tile_size + TILE_GAP, inner_top, TILE_MUTED),        # TR
        (inner_left, inner_top + tile_size + TILE_GAP, TILE_MUTED),        # BL
        (inner_left + tile_size + TILE_GAP,                                # BR: broadcasting
         inner_top + tile_size + TILE_GAP, TILE_BCAST),
    ]

    for (x, y, color) in positions:
        draw.rounded_rectangle(
            [x, y, x + tile_size, y + tile_size],
            radius=TILE_RADIUS,
            fill=color,
        )

    out.parent.mkdir(parents=True, exist_ok=True)
    img.save(out, "PNG")
    print(f"wrote {out} {SIZE}x{SIZE}")


if __name__ == "__main__":
    here = Path(__file__).resolve().parent.parent
    main(here / "src-tauri" / "icons" / "source.png")
