#!/usr/bin/env python3
"""Buat image FB DAS Bengkel Indonesia 11 Sep 2026 — Ciri Bengkel Premium di Surabaya
1080x1350, bg #1a1a2e, teks putih, aksen orange #f97316 (pola gen_das_fb_20260910.py)
Artikel: https://dasindonesia.com/bengkel-premium-terbaik-di-surabaya-ciri-layanan-dan-cara-memilih-yang-tepat
"""
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=74, bold=True):
    """Turunkan ukuran font sampai teks muat dalam max_w."""
    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):
    """Centang vektor (anti-tofu)."""
    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 = "BENGKEL PREMIUM"
t2 = "BUKAN SEKADAR BRANDING"
f1, s1 = fit_font(t1, MAXW, 76)
f2, s2 = fit_font(t2, MAXW, 76)
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 = "6 ciri bengkel yang benar-benar profesional"
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 = [
    ("Spesialisasi, bukan serba bisa", "Fokus engine overhaul & diagnosis presisi"),
    ("Alat ukur presisi", "Micrometer, dial gauge, torque wrench"),
    ("Diagnosis sebelum bongkar", "Dijelaskan dulu, baru dikerjakan"),
    ("Proses transparan", "Tidak ada kejutan di akhir"),
    ("Suku cadang berkualitas & garansi", "Original atau kualitas setara"),
    ("Hasil diuji sebelum serah terima", "Kompresi & performa dicek ulang"),
]
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_20260911.png")
print("saved das_fb_20260911.png")

# ── Sanity check lebar teks (maks aman 950px) ──
checks = [
    ("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_[:18], 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_20260911.png")
print("size:", im.size, "mode:", im.mode)
print("last bullet bottom:", y - step + 44 + 32, "| footer line at:", H - 120)
