#!/usr/bin/env python3
"""Visuel kit social ClickFix : photo réaliste + message central, charte site."""

from __future__ import annotations

import shutil
from pathlib import Path

from PIL import Image, ImageDraw, ImageFilter, ImageFont

ROOT = Path(__file__).resolve().parents[1]
KIT = ROOT / "www/propositions/kit-social-clickfix-prestashop/visuels"
ASSETS = ROOT / "assets"
SRC = KIT / "photo-clickfix-fond.png"
OUT_PNG = KIT / "visuel-clickfix-prestashop.png"
OUT_JPG = KIT / "visuel-clickfix-prestashop.jpg"

W, H = 1920, 1080
ORANGE = (233, 128, 48)
CREAM = (246, 244, 241)
WHITE = (255, 255, 255)

FONT_XB = Path("/tmp/am-fonts/Montserrat-ExtraBold.ttf")
FONT_B = Path("/tmp/am-fonts/Montserrat-Bold.ttf")
FONT_SB = Path("/tmp/am-fonts/Montserrat-SemiBold.ttf")


def cover(img: Image.Image, size: tuple[int, int]) -> Image.Image:
    tw, th = size
    scale = max(tw / img.width, th / img.height)
    resized = img.resize(
        (max(1, int(img.width * scale)), max(1, int(img.height * scale))),
        Image.Resampling.LANCZOS,
    )
    left = (resized.width - tw) // 2
    top = (resized.height - th) // 2
    return resized.crop((left, top, left + tw, top + th))


def font(path: Path, size: int) -> ImageFont.FreeTypeFont:
    return ImageFont.truetype(str(path), size)


def text_size(draw: ImageDraw.ImageDraw, text: str, fnt: ImageFont.FreeTypeFont) -> tuple[int, int]:
    box = draw.textbbox((0, 0), text, font=fnt)
    return box[2] - box[0], box[3] - box[1]


def main() -> None:
    photo = cover(Image.open(SRC).convert("RGB"), (W, H))

    # Soften the laptop screen (garbled fake UI) so the message sits on atmosphere, not on junk type.
    screen = photo.crop((520, 180, 1380, 820)).filter(ImageFilter.GaussianBlur(18))
    photo.paste(screen, (520, 180))

    dark = Image.new("RGB", (W, H), (8, 8, 10))
    photo = Image.blend(photo, dark, 0.28)

    vignette = Image.new("L", (W, H), 0)
    vg = ImageDraw.Draw(vignette)
    vg.ellipse((-180, -220, W + 180, H + 80), fill=255)
    vignette = vignette.filter(ImageFilter.GaussianBlur(90))
    black = Image.new("RGB", (W, H), (4, 4, 6))
    photo = Image.composite(photo, black, vignette)

    overlay = Image.new("RGBA", (W, H), (0, 0, 0, 0))
    plate = Image.new("RGBA", (W, H), (0, 0, 0, 0))
    pd = ImageDraw.Draw(plate)
    # Center glass plate over the screen, slightly taller than the type block.
    card = (430, 250, 1490, 830)
    pd.rounded_rectangle(card, radius=28, fill=(10, 10, 12, 168))
    plate = plate.filter(ImageFilter.GaussianBlur(1))
    overlay = Image.alpha_composite(overlay, plate)

    glass = Image.new("RGBA", (W, H), (0, 0, 0, 0))
    gd = ImageDraw.Draw(glass)
    gd.rounded_rectangle(card, radius=28, outline=(233, 128, 48, 210), width=2)
    overlay = Image.alpha_composite(overlay, glass)

    composed = Image.alpha_composite(photo.convert("RGBA"), overlay)
    draw = ImageDraw.Draw(composed)

    kicker_f = font(FONT_SB, 28)
    l1_f = font(FONT_XB, 78)
    l2_f = font(FONT_XB, 78)
    sub_f = font(FONT_SB, 30)
    foot_f = font(FONT_SB, 26)

    cx = W // 2
    y = 330

    kicker = "REX  ·  JUILLET 2026"
    kw, kh = text_size(draw, kicker, kicker_f)
    draw.text((cx - kw // 2, y), kicker, font=kicker_f, fill=ORANGE)
    y += kh + 36

    l1 = "Ce n'était pas"
    w1, h1 = text_size(draw, l1, l1_f)
    draw.text((cx - w1 // 2, y), l1, font=l1_f, fill=WHITE)
    y += h1 + 8

    l2 = "Cloudflare."
    w2, h2 = text_size(draw, l2, l2_f)
    draw.text((cx - w2 // 2, y), l2, font=l2_f, fill=ORANGE)
    y += h2 + 28

    line_w = 92
    draw.rectangle((cx - line_w // 2, y, cx + line_w // 2, y + 4), fill=ORANGE)
    y += 28

    sub = "Une fausse vérif. Le visiteur colle le malware."
    sw, sh = text_size(draw, sub, sub_f)
    draw.text((cx - sw // 2, y), sub, font=sub_f, fill=CREAM)

    foot = "arnaud-merigeau.fr"
    fw, fh = text_size(draw, foot, foot_f)
    draw.text((cx - fw // 2, 980), foot, font=foot_f, fill=ORANGE)

    final = composed.convert("RGB")
    KIT.mkdir(parents=True, exist_ok=True)
    final.save(OUT_PNG, "PNG", optimize=True)
    final.save(OUT_JPG, "JPEG", quality=92, optimize=True)
    shutil.copy2(OUT_JPG, ASSETS / "visuel-clickfix-prestashop.jpg")
    print(OUT_JPG)
    print(OUT_PNG)


if __name__ == "__main__":
    main()
