#!/usr/bin/env python3
"""Buat image FB DAS Bengkel Indonesia 13 Sep 2026 — "Mobil Toyota Butuh Bengkel Spesialis?"
1080x1350, bg #1a1a2e, teks putih, aksen orange #f97316 (pola gen_das_fb_20260912.py)
Artikel: https://dasindonesia.com/bengkel-spesialis-toyota-surabaya
"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
BG = (26, 26, 46)        # #1a1a2e
TXT = (255, 255, 255)
ORANGE = (249, 115, 22)  # #f97316
MUTED = (200, 200, 210)

FONT_PATHS = [
    "/home/mantapman/.fonts/Poppins-Bold.ttf",
    "/home/mantapman/.fonts/Poppins-SemiBold.ttf",
    "/home/mantapman/.fonts/Poppins-Regular.ttf",
    "/home/mantapman/.fonts/poppins/Poppins-Bold.ttf",
    "/home/mantapman/.fonts/poppins/Poppins-Regular.ttf",
    "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
    "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
]


def font(sz, bold=True, regular=False):
    for p in FONT_PATHS:
        if ("Bold" in p) == bold or (regular and "Regular" in p):
            if os.path.exists(p):
                try:
                    return ImageFont.truetype(p, sz)
                except Exception:
                    continue
    return ImageFont.load_default()


def fit_font(text, max_w, start=80, bold=True):
    sz = start
    while sz > 40:
        f = font(sz, bold)
        if f.getlength(text) <= max_w:
            return f, sz
        sz -= 2
    return font(40, bold), 40


def draw_check(d, x, cy, size, fill):
    s = size
    p1 = (x, cy + s * 0.52)
    p2 = (x + s * 0.32, cy + s * 0.92)
    p3 = (x + s, cy + s * 0.08)
    d.line([p1, p2, p3], fill=fill, width=max(5, int(s * 0.16)), joint="curve")


img = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(img)

SZ = 65
MAXW = W - 2 * SZ  # 950

# ── Header ──
d.text((SZ, SZ), "DAS BENGKEL INDONESIA", font=font(34, True), fill=ORANGE)
d.text((W - SZ, SZ), "OTOMOTIF", font=font(30, True), fill=TXT, anchor="ra")
d.line([(SZ, SZ + 70), (W - SZ, SZ + 70)], fill=ORANGE, width=4)

# ── Title (auto-fit, 2 baris) ──
t1 = "MOBIL TOYOTA"
t2 = "BUTUH BENGKEL SPESIALIS?"
f1, s1 = fit_font(t1, MAXW, 82)
f2, s2 = fit_font(t2, MAXW, 82)
tsz = min(s1, s2)
ftitle = font(tsz, True)
LH = int(tsz * 1.22)
TITLE_Y = 186
d.text((SZ, TITLE_Y), t1, font=ftitle, fill=TXT)
d.text((SZ, TITLE_Y + LH), t2, font=ftitle, fill=TXT)

# ── Subtitle ──
sub = "Kenali gejalanya sebelum jadi besar"
sub_y = TITLE_Y + 2 * LH + 34
d.text((SZ, sub_y), sub, font=font(35, True), fill=ORANGE)

# ── Divider ──
div_y = sub_y + 66
d.line([(SZ, div_y), (SZ + 180, div_y)], fill=ORANGE, width=5)

# ── Bullets (6 item) ──
bullets = [
    ("Getaran halus saat idle", "Busi, injektor, atau engine mounting"),
    ("Transmisi lambat merespons", "Oli transmisi lewat jadwal ganti"),
    ("Bunyi gluduk di kaki-kaki", "Bushing, link stabilizer, shockbreaker"),
    ("Setir bergetar di kecepatan tertentu", "Perlu pemeriksaan menyeluruh"),
    ("Servis berkala sesuai interval", "Oli, filter udara, dan coolant"),
    ("Pilih bengkel yang paham Toyota", "Bukan sekadar bongkar-pasang"),
]
step = 112
y = div_y + 40
for t, s_ in bullets:
    draw_check(d, SZ + 2, y + 2, 30, ORANGE)
    d.text((SZ + 54, y), t, font=font(32, True), fill=TXT)
    d.text((SZ + 54, y + 44), s_, font=font(24, False), fill=MUTED)
    y += step

# ── Footer ──
d.line([(SZ, H - 120), (W - SZ, H - 120)], fill=(70, 70, 100), width=3)
d.text((SZ, H - 98), "www.dasindonesia.com", font=font(30, True), fill=ORANGE)
d.text((W - SZ, H - 98), "selengkapnya di artikel", font=font(28, True), fill=TXT, anchor="ra")

img.save("das_fb_20260913.png")
print("saved das_fb_20260913.png")

# ── Sanity check lebar teks (maks aman 950px) ──
checks = [
    ("header kiri", font(34, True), "DAS BENGKEL INDONESIA"),
    ("header kanan", font(30, True), "OTOMOTIF"),
    ("title L1", ftitle, t1),
    ("title L2", ftitle, t2),
    ("subtitle", font(35, True), sub),
    ("footer kiri", font(30, True), "www.dasindonesia.com"),
    ("footer kanan", font(28, True), "selengkapnya di artikel"),
]
worst = 0
for t, s_ in bullets:
    checks.append(("label: " + t, font(32, True), t))
    checks.append(("sub: " + s_[:20], font(24, False), s_))
for name, f, txt in checks:
    wd = f.getlength(txt)
    worst = max(worst, wd)
    flag = "OK " if wd <= MAXW else "OVERFLOW!"
    print(f"  [{flag}] {name}: {wd:.0f}px")
print("max width:", round(worst), "limit:", MAXW)
im = Image.open("das_fb_20260913.png")
print("size:", im.size, "mode:", im.mode)
print("last bullet bottom:", y - step + 44 + 32, "| footer line at:", H - 120)
