#!/usr/bin/env python3
"""Generate gambar FB DAS Bengkel — 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'
MARGIN = 65

# Coba 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

# Fallback ke DejaVu
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 draw_text_centered(draw, text, font, color, y, max_w):
    """Draw text centered horizontally"""
    lines = []
    words = text.split()
    line = ''
    for w in words:
        test = line + ' ' + w if line else w
        bb = draw.textbbox((0, 0), test, font=font)
        if bb[2] - bb[0] > max_w and line:
            lines.append(line)
            line = w
        else:
            line = test
    if line:
        lines.append(line)
    
    for l in lines:
        bb = draw.textbbox((0, 0), l, font=font)
        tw = bb[2] - bb[0]
        x = (W - tw) // 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)
    
    # Font sizes
    f_big = ImageFont.truetype(poppins_bold, 110)
    f_title = ImageFont.truetype(poppins_bold, 80)
    f_sub = ImageFont.truetype(poppins_semibold, 45)
    f_body = ImageFont.truetype(poppins_regular, 36)
    f_label = ImageFont.truetype(poppins_bold, 38)
    f_logo = ImageFont.truetype(poppins_bold, 28)
    f_footer = ImageFont.truetype(poppins_regular, 24)
    
    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)
    
    # Main title
    y = 300
    y = draw_text_centered(draw, 'MESIN MOBIL', f_big, WHITE, y, max_w)
    y = draw_text_centered(draw, 'OVERHEAT?', f_big, ORANGE, y, max_w)
    
    # Subtitle
    y += 40
    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_text_centered(draw, 'Jangan Anggap Sepele,', f_sub, WHITE, y, max_w)
    y = draw_text_centered(draw, 'Ini Penyebabnya!', f_sub, ORANGE, y, max_w)
    
    # Orange divider
    y += 50
    draw.line([(MARGIN + 100, y), (W - MARGIN - 100, y)], fill=ORANGE, width=3)
    y += 50
    
    # Bullet items
    items = [
        'Kebocoran Coolant / Radiator',
        'Thermostat Macet Posisi Tertutup',
        'Kipas Pendingin Tidak Berfungsi',
        'Water Pump Rusak / Bocor',
    ]
    
    for item in items:
        # Checkmark
        draw.text((MARGIN + 10, y), '☑', fill=ORANGE, font=f_label)
        bb = draw.textbbox((0, 0), item, font=f_body)
        draw.text((MARGIN + 70, y + 2), item, fill=WHITE, font=f_body)
        y += bb[3] - bb[1] + 18
    
    # CTA section
    y = H - MARGIN - 150
    
    # CTA line
    draw.line([(MARGIN, y), (W - MARGIN, y)], fill=ORANGE, width=1)
    y += 20
    
    draw_text_centered(draw, 'Konsultasi Gratis → WA 0852-1211-0314', f_footer, ORANGE, y, max_w)
    y += 35
    
    # Footer
    draw.text((MARGIN, H - MARGIN - 10), 'www.dasindonesia.com', fill='#888888', font=f_footer)
    bb = draw.textbbox((0, 0), '#DASBengkel #EngineOverhaul', font=f_footer)
    draw.text((W - MARGIN - (bb[2]-bb[0]), H - MARGIN - 10), '#DASBengkel #EngineOverhaul', fill='#888888', font=f_footer)
    
    # Save
    path = '/home/mantapman/.hermes/profiles/atun/workspace/das_fb_overheat.png'
    img.save(path)
    print(f'✅ Gambar saved: {path}')
    return path

if __name__ == '__main__':
    generate()
