#!/usr/bin/env python3
"""Mockups portfolio AquaDiveShop — captures + mises en situation (charte océan)."""

from __future__ import annotations

import importlib.util
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
SHOTS = ROOT / "assets" / "aquadiveshop-mockups" / "screenshots"
OUT = ROOT / "assets" / "aquadiveshop-mockups"

# Réutilise le moteur mockup Cabinet Hexagone
_hex_path = ROOT / "scripts" / "generate_cabinet_hexagone_mockups.py"
spec = importlib.util.spec_from_file_location("hex_mockups", _hex_path)
hex_mod = importlib.util.module_from_spec(spec)
assert spec.loader is not None
spec.loader.exec_module(hex_mod)

# Palette AquaDiveShop : navy / jaune / teal océan
PALETTE = [(8, 28, 58), (14, 52, 96), (28, 110, 168), (232, 196, 48)]
ACCENT = (232, 196, 48)  # jaune CTA
OCEAN = (28, 110, 168)

FEATURED_SIZE = hex_mod.FEATURED_SIZE
THUMB_SIZE = hex_mod.THUMB_SIZE


def main() -> None:
    from PIL import Image

    OUT.mkdir(parents=True, exist_ok=True)
    gallery_dir = OUT / "gallery"
    gallery_dir.mkdir(exist_ok=True)

    desktop = Image.open(SHOTS / "desktop-home.png").convert("RGB")
    mobile = Image.open(SHOTS / "mobile-home.png").convert("RGB")
    if (SHOTS / "mobile-home-full.png").exists():
        mobile_full = Image.open(SHOTS / "mobile-home-full.png").convert("RGB")
    else:
        mobile_full = mobile

    # Featured : iMac + capture desktop, fond océan
    featured = hex_mod.compose_portfolio_desktop_featured(
        desktop,
        canvas_size=FEATURED_SIZE,
        palette=PALETTE,
    )
    # Remplace accent teal par jaune via second pass wow browser+phone
    featured_duo = hex_mod.compose_imac_iphone_featured(desktop, mobile, canvas_size=FEATURED_SIZE)
    # Recolor background: regenerate with ocean palette by composing wow desktop
    featured = hex_mod.compose_wow_desktop_featured(
        desktop,
        canvas_size=FEATURED_SIZE,
        palette=PALETTE,
        accent=OCEAN,
        tilt=-0.4,
    )
    featured_out = OUT / "aquadiveshop-featured.jpg"
    featured.save(featured_out, "JPEG", quality=93, optimize=True, subsampling=0)
    print(f"saved {featured_out.name} {featured.size}")

    thumb = featured.resize(THUMB_SIZE, Image.Resampling.LANCZOS)
    thumb_out = OUT / "aquadiveshop-thumb.jpg"
    thumb.save(thumb_out, "JPEG", quality=90, optimize=True)
    print(f"saved {thumb_out.name}")

    duo_out = OUT / "aquadiveshop-desktop-mobile.jpg"
    # Rebuild duo with ocean mesh by monkey-patching FEATURED_PALETTE temporarily
    old = hex_mod.FEATURED_PALETTE
    hex_mod.FEATURED_PALETTE = PALETTE
    duo = hex_mod.compose_imac_iphone_featured(desktop, mobile, canvas_size=FEATURED_SIZE)
    hex_mod.FEATURED_PALETTE = old
    duo.save(duo_out, "JPEG", quality=92, optimize=True)
    print(f"saved {duo_out.name}")

    # Mobile hero mockup
    mobile_mock = hex_mod.phone_mockup(mobile, title="AquaDiveShop")
    mobile_out = OUT / "aquadiveshop-mobile.jpg"
    mobile_mock.save(mobile_out, "JPEG", quality=90, optimize=True)
    print(f"saved {mobile_out.name}")

    # Gallery shots (captures brutes cropées)
    gallery_jobs = [
        ("desktop-home.png", "aquadiveshop-accueil.jpg", 1700),
        ("desktop-categorie.png", "aquadiveshop-categorie.jpg", 1700),
        ("desktop-pack.png", "aquadiveshop-packs.jpg", 1700),
        ("desktop-guide.png", "aquadiveshop-guide.jpg", 1700),
        ("desktop-masques.png", "aquadiveshop-masques.jpg", 1700),
        ("mobile-home-full.png", "aquadiveshop-mobile-shot.jpg", 3200),
    ]
    for src_name, out_name, max_h in gallery_jobs:
        src = SHOTS / src_name
        if not src.exists():
            print(f"skip missing {src_name}")
            continue
        hex_mod.export_gallery_shot(src, gallery_dir / out_name, max_height=max_h)
        print(f"saved gallery/{out_name}")

    # Also put duo in gallery as first visual wow
    duo.save(gallery_dir / "aquadiveshop-devices.jpg", "JPEG", quality=92, optimize=True)
    print("saved gallery/aquadiveshop-devices.jpg")


if __name__ == "__main__":
    main()
