#!/usr/bin/env python3
"""Buat image FB DAS Bengkel Indonesia 9 Sep 2026 — 6 Tanda Bahaya Check Engine Menyala
1080x1350, bg #1a1a2e, teks putih, aksen orange #f97316 (pola gen_das_fb_20260908.py)
Artikel: https://dasindonesia.com/6-tanda-bahaya-check-engine-menyala-yang-harus-diwaspadai/
"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
BG = (26, 26, 46)        # #1a1a2e
TXT = (255, 255, 255)    # white
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 draw_check(d, x, cy, size, fill):
    """Gambar centang vektor (garis 2 segmen) — 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

# 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")

# Accent line under header
d.line([(SZ, SZ + 70), (W - SZ, SZ + 70)], fill=ORANGE, width=4)

# Title (big, bold, 2 baris)
title = "6 TANDA BAHAYA\nCHECK ENGINE NYALA"
d.multiline_text((SZ, 185), title, font=font(84, True), fill=TXT, spacing=12)

# Subtitle (orange)
d.text((SZ, 545), "Jangan anggap remeh lampu kuning ini", font=font(38, True), fill=ORANGE)

# Garis aksen kecil sebelum bullets
d.line([(SZ, 635), (SZ + 180, 635)], fill=ORANGE, width=5)

# Body bullets — 4 item, centang vektor orange
bullets = [
    ("Mesin brebet & bergetar", "Idle tidak stabil, kabin ikut terasa"),
    ("BBM melonjak tiba-tiba", "Boros tanpa perubahan pola berkendara"),
    ("Tenaga mesin menurun", "Akselerasi lemah saat digas"),
    ("Lampu berkedip cepat", "Tanda serius — segera perlu diperiksa"),
]
y = 690
for t, s_ in bullets:
    draw_check(d, SZ + 4, y + 6, 34, ORANGE)
    d.text((SZ + 62, y), t, font=font(36, True), fill=TXT)
    d.text((SZ + 62, y + 52), s_, font=font(26, False), fill=MUTED)
    y += 140

# Note bar
d.text((SZ, y + 15), "Simak 6 tanda lengkap & langkah tepat di artikel kami", font=font(29, True), fill=ORANGE)

# Footer
d.line([(SZ, H - 130), (W - SZ, H - 130)], fill=(70, 70, 100), width=3)
d.text((SZ, H - 108), "www.dasindonesia.com", font=font(30, True), fill=ORANGE)
d.text((W - SZ, H - 108), "#checkengine", font=font(30, True), fill=TXT, anchor="ra")

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

# Sanity check: lebar teks terpanjang (max aman ~950px)
checks = [
    ("title S1", font(84, True), "6 TANDA BAHAYA"),
    ("title S2", font(84, True), "CHECK ENGINE NYALA"),
    ("subtitle", font(38, True), "Jangan anggap remeh lampu kuning ini"),
    ("note", font(29, True), "Simak 6 tanda lengkap & langkah tepat di artikel kami"),
    ("b1 label", font(36, True), "Mesin brebet & bergetar"),
    ("b1 sub", font(26, False), "Idle tidak stabil, kabin ikut terasa"),
    ("footer l", font(30, True), "www.dasindonesia.com"),
    ("footer r", font(30, True), "#checkengine"),
]
for name, f, txt in checks:
    print(f"  {name}: {f.getlength(txt):.0f}px")

im = Image.open("das_fb_20260909.png")
print("size:", im.size, "mode:", im.mode)
