#!/usr/bin/env python3
"""Generate gambar FB DAS Bengkel — 21 Agustus 2026
Artikel: Servis Rutin Mobil Alphard
1080x1350, bg #1a1a2e, text putih, aksen #f97316
"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
BG = '#1a1a2e'
ORANGE = '#f97316'
WHITE = '#ffffff'
GRAY = '#888888'
MARGIN = 65

# Cari font Poppins
font_dirs = [
    '/usr/share/fonts',
    '/usr/local/share/fonts',
    os.path.expanduser('~/.fonts'),
    os.path.expanduser('~/.local/share/fonts'),
]
poppins_bold = poppins_semibold = poppins_regular = None

for d in font_dirs:
    for root, dirs, files in os.walk(d):
        for f in files:
            p = os.path.join(root, f)
            if 'Poppins-Bold' in f and f.endswith(('.ttf', '.otf')):
                poppins_bold = p
            elif 'Poppins-SemiBold' in f and f.endswith(('.ttf', '.otf')):
                poppins_semibold = p
            elif 'Poppins-Regular' in f and f.endswith(('.ttf', '.otf')):
                poppins_regular = p

if not poppins_bold:
    poppins_bold = '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'
if not poppins_semibold:
    poppins_semibold = poppins_bold
if not poppins_regular:
    poppins_regular = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'

def text_w(draw, text, font):
    bb = draw.textbbox((0, 0), text, font=font)
    return bb[2] - bb[0]

def text_h(draw, text, font):
    bb = draw.textbbox((0, 0), text, font=font)
    return bb[3] - bb[1]

def wrap_text(draw, text, font, max_w):
    words = text.split()
    lines = []
    line = ''
    for w in words:
        test = line + ' ' + w if line else w
        if text_w(draw, test, font) > max_w and line:
            lines.append(line)
            line = w
        else:
            line = test
    if line:
        lines.append(line)
    return lines

def draw_centered(draw, text, font, color, y, max_w):
    lines = wrap_text(draw, text, font, max_w)
    for l in lines:
        tw = text_w(draw, l, font)
        x = (W - tw) // 2
        draw.text((x, y), l, fill=color, font=font)
        y += text_h(draw, l, font) + 8
    return y

def generate():
    img = Image.new('RGB', (W, H), BG)
    draw = ImageDraw.Draw(img)

    f_title = ImageFont.truetype(poppins_bold, 96)
    f_sub = ImageFont.truetype(poppins_semibold, 40)
    f_body = ImageFont.truetype(poppins_regular, 34)
    f_bullet = ImageFont.truetype(poppins_semibold, 33)
    f_logo = ImageFont.truetype(poppins_bold, 24)
    f_footer = ImageFont.truetype(poppins_regular, 22)

    max_w = W - MARGIN * 2

    # Header
    draw.text((MARGIN, MARGIN), 'DAS BENGKEL INDONESIA', fill=ORANGE, font=f_logo)
    bb = draw.textbbox((0, 0), 'SPESIALIS ENGINE', font=f_logo)
    draw.text((W - MARGIN - (bb[2]-bb[0]), MARGIN), 'SPESIALIS ENGINE', fill=ORANGE, font=f_logo)

    # Divider
    y = MARGIN + 40
    draw.line([(MARGIN, y), (W - MARGIN, y)], fill=ORANGE, width=2)

    # Title block
    y = 300
    y = draw_centered(draw, 'SERVIS RUTIN', f_title, WHITE, y, max_w)
    y = draw_centered(draw, 'MOBIL ALPHARD', f_title, ORANGE, y, max_w)
    y += 20
    y = draw_centered(draw, 'Kunci Kenyamanan', f_sub, WHITE, y, max_w)
    y = draw_centered(draw, 'MPV Premium', f_sub, ORANGE, y, max_w)

    # Divider
    y += 50
    draw.line([(MARGIN + 100, y), (W - MARGIN - 100, y)], fill=ORANGE, width=3)
    y += 50

    # Bullet items
    items = [
        'Oli Mesin & Filter Berkala',
        'Kaki-Kaki & Shock Breaker',
        'Sistem Rem & Minyak Rem',
        'Sistem Pendingin / Coolant',
        'Bushing & Komponen Karet',
        'Diagnosis Khusus MPV Premium',
    ]

    for item in items:
        draw.text((MARGIN + 10, y), '☑', fill=ORANGE, font=f_sub)
        bb = draw.textbbox((0, 0), item, font=f_bullet)
        draw.text((MARGIN + 70, y + 2), item, fill=WHITE, font=f_bullet)
        y += text_h(draw, item, f_bullet) + 20

    # CTA
    y = H - MARGIN - 150
    draw.line([(MARGIN, y), (W - MARGIN, y)], fill=ORANGE, width=1)
    y += 20

    cta = 'Konsultasi Gratis → 0851-1666-7890'
    bb = draw.textbbox((0, 0), cta, font=f_body)
    draw.text(((W - (bb[2]-bb[0]))//2, y), cta, fill=ORANGE, font=f_body)
    y += 40

    # Footer
    draw.text((MARGIN, H - MARGIN - 10), 'www.dasindonesia.com', fill=GRAY, font=f_footer)
    bb = draw.textbbox((0, 0), '#DASBengkel #ServisAlphard', font=f_footer)
    draw.text((W - MARGIN - (bb[2]-bb[0]), H - MARGIN - 10), '#DASBengkel #ServisAlphard', fill=GRAY, font=f_footer)

    path = '/home/mantapman/.hermes/profiles/atun/workspace/das_fb_20260821.png'
    img.save(path)
    print(f'✅ Gambar saved: {path}')
    return path

if __name__ == '__main__':
    generate()
