#!/usr/bin/env python3
"""Generate gambar FB DAS 2026-08-05 — Cara Merawat Transmisi Matic
1080x1350, bg #1a1a2e, teks putih, aksen #f97316"""
from PIL import Image, ImageDraw, ImageFont
import os

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

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 wrap(draw, text, font, max_w):
    lines, line = [], ''
    for w in text.split():
        t = line + ' ' + w if line else w
        if draw.textbbox((0, 0), t, font=font)[2] > max_w and line:
            lines.append(line); line = w
        else:
            line = t
    if line: lines.append(line)
    return lines

def draw_center(draw, text, font, color, y, max_w):
    for l in wrap(draw, text, font, max_w):
        bb = draw.textbbox((0, 0), l, font=font)
        x = (W - (bb[2]-bb[0])) // 2
        draw.text((x, y), l, fill=color, font=font)
        y += bb[3] - bb[1] + 8
    return y

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

    f_logo = ImageFont.truetype(poppins_bold, 26)
    f_label = ImageFont.truetype(poppins_bold, 38)
    f_title = ImageFont.truetype(poppins_bold, 68)
    f_sub = ImageFont.truetype(poppins_semibold, 40)
    f_body = ImageFont.truetype(poppins_regular, 34)
    f_footer = ImageFont.truetype(poppins_regular, 24)
    max_w = W - MARGIN * 2

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

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

    # Title
    y = draw_center(draw, 'TRANSMISI MATIC', f_title, WHITE, y, max_w)
    y = draw_center(draw, 'AWET ATAU CEPAT RUSAK?', f_title, ORANGE, y, max_w)

    y += 30
    bb = draw.textbbox((0, 0), '— — — — — — — — — — —', font=f_sub)
    draw.text(((W - (bb[2]-bb[0]))//2, y), '— — — — — — — — — — —', fill=ORANGE, font=f_sub)
    y += 50

    y = draw_center(draw, 'Kunci Perawatan yang', f_sub, WHITE, y, max_w)
    y = draw_center(draw, 'Sering Dilupakan', f_sub, ORANGE, y, max_w)

    y += 40
    draw.line([(MARGIN + 100, y), (W - MARGIN - 100, y)], fill=ORANGE, width=3)
    y += 46

    items = [
        'Kenali Jenis AT / CVT / DCT',
        'Ganti Oli Tepat Jadwal',
        'Hindari Kebiasaan Buruk Berkendara',
        'Tangani Gejala Sejak Dini',
    ]
    for item in items:
        draw.text((MARGIN + 10, y), '☑', fill=ORANGE, font=f_label)
        for i, l in enumerate(wrap(draw, item, f_body, max_w - 110)):
            draw.text((MARGIN + 70, y + i * 44), l, fill=WHITE, font=f_body)
        y += 52 if len(wrap(draw, item, f_body, max_w - 110)) == 1 else 96

    # CTA
    y = H - MARGIN - 150
    draw.line([(MARGIN, y), (W - MARGIN, y)], fill=ORANGE, width=1)
    y += 20
    y = draw_center(draw, 'Konsultasi Gratis → WA 0852-1211-0314', f_footer, ORANGE, y, max_w)
    y += 35

    # Footer
    fh = draw.textbbox((0, 0), 'Ag', font=f_footer)[3]
    base_y = H - SAFE - fh
    draw.text((MARGIN, base_y), 'www.dasindonesia.com', fill=GREY, font=f_footer)
    bb = draw.textbbox((0, 0), '#DASBengkel #TransmisiMatic', font=f_footer)
    draw.text((W - SAFE - (bb[2]-bb[0]), base_y), '#DASBengkel #TransmisiMatic', fill=GREY, font=f_footer)

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

if __name__ == '__main__':
    generate()
