#!/usr/bin/env python3
"""Generate FB DAS image 1080x1350 — bg gelap #1a1a2e, teks putih, aksen #f97316"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
BG = (26, 26, 46)        # #1a1a2e
WHITE = (255, 255, 255)
ORANGE = (249, 115, 22)  # #f97316
GRAY = (200, 200, 215)

OUT = "/home/mantapman/.hermes/profiles/atun/workspace/das_fb_20260806_alphard.png"

# Font paths (Poppins)
FONT_DIR = "/usr/share/fonts/truetype"
def find_font(names):
    for n in names:
        p = os.path.join(FONT_DIR, n)
        if os.path.exists(p):
            return p
    return None

bold = find_font(["poppins/Poppins-Bold.ttf", "Poppins-Bold.ttf"]) or "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
semibold = find_font(["poppins/Poppins-SemiBold.ttf", "Poppins-SemiBold.ttf"]) or bold
regular = find_font(["poppins/Poppins-Regular.ttf", "Poppins-Regular.ttf"]) or "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"

img = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(img)

# Safe zone
SZ = 60

def draw_text_block(draw, xy, text, font, fill, spacing=14, max_width=None, anchor=None):
    x, y = xy
    lines = []
    for para in text.split("\n"):
        words = para.split(" ")
        cur = ""
        for w in words:
            t = (cur + " " + w).strip()
            if max_width and draw.textlength(t, font=font) > max_width:
                lines.append(cur)
                cur = w
            else:
                cur = t
        lines.append(cur)
    for ln in lines:
        draw.text((x, y), ln, font=font, fill=fill, anchor=anchor)
        y += draw.textlength(ln, font=font) * 0 + (draw.textbbox((0,0), ln, font=font)[3] - draw.textbbox((0,0), ln, font=font)[1]) + spacing
    return y

def line_height(font, text="Ay"):
    bb = d.textbbox((0,0), text, font=font)
    return bb[3]-bb[1]

# ===== Layout =====
y = SZ

# Header: DAS BENGKEL INDONESIA
d.text((SZ, y), "DAS BENGKEL", font=ImageFont.truetype(bold, 42), fill=ORANGE)
d.text((SZ, y+52), "INDONESIA", font=ImageFont.truetype(bold, 42), fill=WHITE)
y += 145

# Orange accent bar
d.rectangle([SZ, y, SZ+90, y+10], fill=ORANGE)
y += 40

# Title
title_font = ImageFont.truetype(bold, 88)
title = "7 MASALAH\nUMUM MESIN\nALPHARD"
lh = line_height(title_font)
for i, ln in enumerate(title.split("\n")):
    d.text((SZ, y), ln, font=title_font, fill=WHITE)
    y += lh + 8
y += 30

# Subtitle
sub_font = ImageFont.truetype(regular, 40)
d.text((SZ, y), "Dan cara mengatasinya", font=sub_font, fill=GRAY)
y += 70

# Divider
d.rectangle([SZ, y, W-SZ, y+3], fill=ORANGE)
y += 60

# Checkpoints
pt_font = ImageFont.truetype(regular, 36)
check_items = [
    "Kenali gejala sejak dini",
    "Overheat berulang & oli cepat habis",
    "Cek kompresi sebelum ganti part",
    "Hindari biaya overhaul mahal",
]
for item in check_items:
    d.ellipse([SZ+6, y+16, SZ+30, y+40], fill=ORANGE)
    d.text((SZ+52, y), item, font=pt_font, fill=WHITE)
    y += line_height(pt_font) + 30
y += 20

# CTA
d.text((SZ, y), "Baca selengkapnya di", font=sub_font, fill=GRAY)
y += 52
d.text((SZ, y), "dasindonesia.com", font=ImageFont.truetype(bold, 52), fill=ORANGE)
y += 80

# Footer
d.text((SZ, H-SZ-40), "Spesialis Engine Overhaul & Repair", font=ImageFont.truetype(regular, 30), fill=GRAY)
d.text((W-SZ, H-SZ-40), "DAS", font=ImageFont.truetype(bold, 30), fill=ORANGE, anchor="ra")

img.save(OUT)
print("Saved:", OUT, img.size)
