#!/usr/bin/env python3
"""Build app/static/icons/cnp-solutions-logo.ico from the CNP Solutions logo JPEG."""
from pathlib import Path

from PIL import Image

ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "app" / "static" / "images" / "cnp-solutions-logo.jpg"
if not SRC.exists():
    SRC = ROOT / "CNP Solutions logo.jpg"
OUT = ROOT / "app" / "static" / "icons" / "cnp-solutions-logo.ico"


def main() -> None:
    img = Image.open(SRC).convert("RGBA")
    w, h = img.size
    side = min(w, h)
    left = (w - side) // 2
    top = (h - side) // 2
    img = img.crop((left, top, left + side, top + side))
    sizes = [256, 128, 64, 48, 32, 16]
    icons = [img.resize((s, s), Image.Resampling.LANCZOS) for s in sizes]
    OUT.parent.mkdir(parents=True, exist_ok=True)
    icons[0].save(OUT, format="ICO", sizes=[(s, s) for s in sizes])
    print(f"Wrote {OUT} ({OUT.stat().st_size} bytes)")


if __name__ == "__main__":
    main()
