#!/usr/bin/env python3
"""Visuels inline article PrestaShop vs Shopify : TCO 3 ans + score comparatif."""

from __future__ import annotations

from pathlib import Path

from PIL import Image, ImageDraw, ImageFont

ROOT = Path(__file__).resolve().parents[1]
ASSETS = ROOT / "assets"

ORANGE = (233, 128, 48)
GREEN = (149, 191, 71)
INK = (26, 26, 26)
MUTED = (102, 102, 102)
WHITE = (255, 255, 255)
LIGHT = (248, 246, 242)
GRID = (230, 226, 218)


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


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


def render_tco_chart() -> Image.Image:
    w, h = 1200, 680
    img = Image.new("RGB", (w, h), LIGHT)
    draw = ImageDraw.Draw(img)

    title_f = load_font(36, True)
    sub_f = load_font(22)
    label_f = load_font(24, True)
    val_f = load_font(28, True)
    note_f = load_font(18)

    draw.text((60, 40), "TCO sur 3 ans (150 000 € CA/an)", fill=INK, font=title_f)
    draw.text((60, 88), "Source : modèle Neocell · PME e-commerce type", fill=MUTED, font=sub_f)

    chart_top, chart_bottom = 160, 520
    chart_left, chart_right = 120, 1080
    max_val = 20000

    ps_val, sh_val = 8800, 19350
    bar_w = 180
    cx_ps, cx_sh = 420, 780
    scale = (chart_bottom - chart_top) / max_val

    draw.line([(chart_left, chart_bottom), (chart_right, chart_bottom)], fill=GRID, width=2)
    for tick in (0, 5000, 10000, 15000, 20000):
        y = chart_bottom - int(tick * scale)
        draw.line([(chart_left, y), (chart_right, y)], fill=GRID, width=1)
        draw.text((70, y - 10), f"{tick // 1000} k€", fill=MUTED, font=note_f)

    def bar(cx: int, val: int, color: tuple[int, int, int], label: str) -> None:
        bh = int(val * scale)
        top = chart_bottom - bh
        draw.rounded_rectangle((cx - bar_w // 2, top, cx + bar_w // 2, chart_bottom), radius=16, fill=color)
        draw.text((cx, top - 36), f"{val:,} €".replace(",", " "), fill=INK, font=val_f, anchor="mm")
        draw.text((cx, chart_bottom + 28), label, fill=INK, font=label_f, anchor="mm")

    bar(cx_ps, ps_val, ORANGE, "PrestaShop")
    bar(cx_sh, sh_val, GREEN, "Shopify")

    savings = sh_val - ps_val
    draw.rounded_rectangle((60, 560, 1140, 640), radius=18, fill=(255, 244, 235))
    draw.text(
        (600, 600),
        f"Écart : {savings:,} € économisés sur 3 ans avec PrestaShop (hors commissions Shopify)".replace(",", " "),
        fill=ORANGE,
        font=load_font(22, True),
        anchor="mm",
    )
    return img


def render_score_chart() -> Image.Image:
    w, h = 1200, 620
    img = Image.new("RGB", (w, h), LIGHT)
    draw = ImageDraw.Draw(img)

    title_f = load_font(36, True)
    sub_f = load_font(22)
    big_f = load_font(120, True)
    lbl_f = load_font(26, True)

    draw.text((60, 40), "Score comparatif PME française (15 critères)", fill=INK, font=title_f)
    draw.text((60, 88), "PrestaShop domine sur TCO, SEO, B2B, conformité FR et propriété des données", fill=MUTED, font=sub_f)

    draw_card_bg(draw, (80, 150, 520, 560))
    draw_card_bg(draw, (680, 150, 1120, 560))

    draw.text((300, 280), "12", fill=ORANGE, font=big_f, anchor="mm")
    draw.text((300, 380), "PrestaShop", fill=INK, font=lbl_f, anchor="mm")
    draw.text((300, 430), "avantages terrain", fill=MUTED, font=load_font(20), anchor="mm")

    draw.text((900, 280), "3", fill=GREEN, font=big_f, anchor="mm")
    draw.text((900, 380), "Shopify", fill=INK, font=lbl_f, anchor="mm")
    draw.text((900, 430), "time-to-market · zero ops", fill=MUTED, font=load_font(20), anchor="mm")

    draw.text((600, 520), "Shopify : pertinent surtout pour un test D2C sans ERP ni SEO existant", fill=MUTED, font=load_font(20), anchor="mm")
    return img


def main() -> int:
    ASSETS.mkdir(parents=True, exist_ok=True)
    tco = ASSETS / "prestashop-shopify-tco-2026.png"
    score = ASSETS / "prestashop-shopify-score-2026.png"
    render_tco_chart().save(tco, "PNG", optimize=True)
    render_score_chart().save(score, "PNG", optimize=True)
    print(f"Visuels article : {tco.name}, {score.name}")
    return 0


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