#!/usr/bin/env python3
"""Visuels article reCAPTCHA PrestaShop 8 / 9 — charte arnaud-merigeau.fr."""

from __future__ import annotations

from pathlib import Path

from PIL import Image, ImageDraw, ImageFilter, ImageFont

ROOT = Path(__file__).resolve().parents[1]
ASSETS = ROOT / "assets"
SOCIAL = ROOT / "www/propositions/kit-social-recaptcha-prestashop-2026/visuels"

ORANGE = (233, 128, 48)
PEACH = (255, 202, 161)
INK = (26, 26, 26)
MUTED = (102, 102, 102)
WHITE = (255, 255, 255)
CREAM = (255, 252, 248)
CARD = (255, 255, 255)
GRID = (230, 226, 218)
GREEN = (46, 125, 50)
NAVY = (18, 32, 56)


def load_font(size: int, *, bold: bool = False, black: bool = False) -> ImageFont.ImageFont:
    if black:
        candidates = [
            "/System/Library/Fonts/Supplemental/Arial Black.ttf",
            "/Library/Fonts/Arial Black.ttf",
            "/System/Library/Fonts/Supplemental/Arial Bold.ttf",
        ]
    elif bold:
        candidates = [
            "/System/Library/Fonts/Supplemental/Arial Bold.ttf",
            "/Library/Fonts/Arial Bold.ttf",
        ]
    else:
        candidates = [
            "/System/Library/Fonts/Supplemental/Arial.ttf",
            "/Library/Fonts/Arial.ttf",
        ]
    for path in candidates:
        if Path(path).exists():
            return ImageFont.truetype(path, size)
    return ImageFont.load_default()


def add_blur_halo(
    base: Image.Image,
    center: tuple[int, int],
    radius: int,
    color: tuple[int, int, int, int],
    blur: int,
) -> Image.Image:
    layer = Image.new("RGBA", base.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(layer)
    cx, cy = center
    draw.ellipse((cx - radius, cy - radius, cx + radius, cy + radius), fill=color)
    layer = layer.filter(ImageFilter.GaussianBlur(radius=blur))
    return Image.alpha_composite(base.convert("RGBA"), layer)


def cream_bg(w: int, h: int) -> Image.Image:
    img = Image.new("RGB", (w, h), CREAM)
    draw = ImageDraw.Draw(img)
    for y in range(h):
        t = y / h
        draw.line([(0, y), (w, y)], fill=(255, int(252 - t * 6), int(248 - t * 10)))
    img = add_blur_halo(img, (w - 180, 140), 280, (233, 128, 48, 70), blur=90)
    img = add_blur_halo(img, (160, h - 80), 240, (255, 190, 130, 55), blur=80)
    return img.convert("RGB")


def rounded_card(draw: ImageDraw.ImageDraw, box: tuple[int, int, int, int]) -> None:
    draw.rounded_rectangle(box, radius=22, fill=WHITE, outline=GRID, width=2)


def render_featured() -> Image.Image:
    w, h = 1200, 675
    img = cream_bg(w, h)
    draw = ImageDraw.Draw(img)

    badge_f = load_font(22, bold=True)
    title_f = load_font(62, black=True)
    sub_f = load_font(32, bold=True)
    pill_f = load_font(22, bold=True)
    foot_f = load_font(20)

    draw.rounded_rectangle((56, 48, 330, 102), radius=16, fill=ORANGE)
    draw.text((193, 75), "TUTO GRATUIT 2026", fill=WHITE, font=badge_f, anchor="mm")

    draw.text((56, 148), "reCAPTCHA", fill=INK, font=title_f)
    draw.text((56, 228), "PrestaShop 8 et 9", fill=ORANGE, font=title_f)
    draw.text((56, 312), "Contact · inscription · newsletter", fill=MUTED, font=sub_f)
    draw.text((56, 354), "Sans module payant", fill=MUTED, font=load_font(26, bold=True))

    pills = ["PS 1.7", "PS 8", "PS 9", "Hummingbird"]
    x = 56
    for pill in pills:
        tw = draw.textlength(pill, font=pill_f)
        draw.rounded_rectangle((x, 410, x + tw + 40, 462), radius=14, fill=NAVY)
        draw.text((x + 20, 436), pill, fill=WHITE, font=pill_f, anchor="lm")
        x += tw + 56

    # Fake checkbox widget
    rounded_card(draw, (820, 130, 1144, 430))
    draw.rounded_rectangle((852, 178, 916, 242), radius=8, outline=GRID, width=3)
    draw.line([(866, 210), (882, 226), (904, 190)], fill=GREEN, width=6)
    draw.text((936, 210), "Je ne suis pas un robot", fill=INK, font=load_font(20, bold=True), anchor="lm")
    draw.text((982, 380), "reCAPTCHA v2", fill=MUTED, font=load_font(16), anchor="mm")

    draw.text((56, 620), "arnaud-merigeau.fr  ·  freelance PrestaShop certifié Experts", fill=MUTED, font=foot_f)
    return img


def render_steps() -> Image.Image:
    w, h = 1200, 680
    img = cream_bg(w, h)
    draw = ImageDraw.Draw(img)
    title_f = load_font(34, black=True)
    num_f = load_font(36, black=True)
    h_f = load_font(24, bold=True)
    p_f = load_font(18)

    draw.text((600, 52), "3 étapes pour bloquer le spam", fill=INK, font=title_f, anchor="mm")
    draw.text((600, 98), "PrestaShop 8 et 9 — sans modifier le cœur", fill=MUTED, font=load_font(20), anchor="mm")

    cards = [
        ("1", "Clés Google", "Console reCAPTCHA\n(case à cocher v2\nou score v3)"),
        ("2", "Module gratuit", "EiCaptcha 2.6 (PS 8)\nou module déclaré\ncompatible PS 9"),
        ("3", "Tests réels", "Incognito + mobile\nSans cocher = bloqué\nCache vidé"),
    ]
    for i, (num, title, body) in enumerate(cards):
        x0 = 48 + i * 384
        rounded_card(draw, (x0, 160, x0 + 352, 600))
        draw.ellipse((x0 + 140, 196, x0 + 212, 268), fill=ORANGE)
        draw.text((x0 + 176, 232), num, fill=WHITE, font=num_f, anchor="mm")
        draw.text((x0 + 176, 320), title, fill=INK, font=h_f, anchor="mm")
        y = 370
        for line in body.split("\n"):
            draw.text((x0 + 176, y), line, fill=MUTED, font=p_f, anchor="mm")
            y += 32
    return img


def render_compat() -> Image.Image:
    w, h = 1200, 680
    img = cream_bg(w, h)
    draw = ImageDraw.Draw(img)
    title_f = load_font(32, black=True)
    head_f = load_font(20, bold=True)
    cell_f = load_font(18)

    draw.text((600, 48), "Quel module selon ta version PrestaShop ?", fill=INK, font=title_f, anchor="mm")

    rows = [
        ("Version", "Module gratuit", "Formulaires", "À retenir"),
        ("PS 1.7 / 8", "EiCaptcha 2.6.0", "Contact, compte,\nnewsletter", "ZIP GitHub Releases\n(pas Download ZIP)"),
        ("PS 9", "Module déclaré 9.x\n(Tec Spam Guard,\nPrestaHero free…)", "Contact, compte,\nlogin, parfois checkout", "EiCaptcha 3.x en cours\nVérifier la fiche 9.x"),
        ("PS 1.6", "EiCaptcha 0.4.x", "Contact", "Mieux : migrer\nvers 8 / 9"),
    ]

    top, row_h, left, widths = 110, 130, 40, [180, 320, 300, 320]
    y = top
    for r, row in enumerate(rows):
        x = left
        bg = ORANGE if r == 0 else WHITE
        fg = WHITE if r == 0 else INK
        for i, (cell, width) in enumerate(zip(row, widths)):
            box = (x, y, x + width - 8, y + row_h - 10)
            draw.rounded_rectangle(box, radius=12, fill=bg, outline=GRID if r else ORANGE, width=2)
            font = head_f if r == 0 else cell_f
            lines = cell.split("\n")
            cy = y + (row_h - 10) / 2 - (len(lines) - 1) * 12
            for line in lines:
                draw.text((x + width / 2 - 4, cy), line, fill=fg if r == 0 else (INK if i == 0 else MUTED), font=font, anchor="mm")
                cy += 24
            x += width
        y += row_h
    return img


def render_social() -> Image.Image:
    w, h = 1920, 1080
    img = cream_bg(w, h)
    img = add_blur_halo(img, (1400, 280), 420, (233, 128, 48, 70), blur=110)
    img = img.convert("RGB")
    draw = ImageDraw.Draw(img)
    badge_f = load_font(28, bold=True)
    title_f = load_font(72, black=True)
    sub_f = load_font(34)
    foot_f = load_font(24, bold=True)

    draw.rounded_rectangle((80, 72, 430, 140), radius=18, fill=ORANGE)
    draw.text((255, 106), "CAS CLIENT", fill=WHITE, font=badge_f, anchor="mm")

    draw.text((80, 240), "200 comptes bots", fill=INK, font=title_f)
    draw.text((80, 340), "en 48 h. Zéro CAPTCHA.", fill=INK, font=title_f)
    draw.text((80, 470), "reCAPTCHA + honeypot sur PrestaShop 8 et 9.", fill=ORANGE, font=sub_f)
    draw.text((80, 530), "Tuto gratuit, sans module payant.", fill=MUTED, font=sub_f)

    draw.rounded_rectangle((80, 640, 720, 720), radius=18, fill=NAVY)
    draw.text((400, 680), "arnaud-merigeau.fr/ajouter-recaptcha-prestashop-tuto-gratuit", fill=WHITE, font=foot_f, anchor="mm")

    rounded_card(draw, (1280, 220, 1840, 780))
    draw.rounded_rectangle((1360, 340, 1480, 460), radius=12, outline=GRID, width=4)
    draw.line([(1390, 400), (1420, 430), (1458, 360)], fill=GREEN, width=10)
    draw.text((1560, 400), "Je ne suis pas\nun robot", fill=INK, font=load_font(28, bold=True), anchor="lm")
    draw.text((1560, 640), "PS 8  ·  PS 9", fill=ORANGE, font=load_font(26, bold=True), anchor="mm")
    return img


def main() -> int:
    ASSETS.mkdir(parents=True, exist_ok=True)
    SOCIAL.mkdir(parents=True, exist_ok=True)

    featured = render_featured()
    steps = render_steps()
    compat = render_compat()
    social = render_social()

    paths = {
        ASSETS / "recaptcha-prestashop-2026-featured.png": featured,
        ASSETS / "recaptcha-prestashop-2026-etapes.png": steps,
        ASSETS / "recaptcha-prestashop-2026-compatibilite.png": compat,
        SOCIAL / "visuel-recaptcha-prestashop-2026.png": social,
        SOCIAL / "visuel-recaptcha-prestashop-2026.jpg": social,
    }
    for path, img in paths.items():
        if path.suffix.lower() in {".jpg", ".jpeg"}:
            img.convert("RGB").save(path, "JPEG", quality=92, optimize=True)
        else:
            img.save(path, "PNG", optimize=True)
        print(f"✓ {path}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
