#!/usr/bin/env python3
"""
Generate slide FB DMark 2026-08-16 — Brand Awareness (BG Orange template)
Resolusi: 1080x1350 (4:5) | BG #F97316 | Teks putih | Poppins
Safe zone 20px | Margin 65px | Vertically middle | No boxes
"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
ORANGE = (249, 115, 22)
WHITE = (255, 255, 255)
M = 65      # margin desain
S = 20      # safe zone minimal

FONT_DIR = os.path.expanduser("~/.fonts")

def F(weight, size):
    return ImageFont.truetype(os.path.join(FONT_DIR, f"Poppins-{weight}.ttf"), size)

B24 = F("Bold", 24)
R22 = F("Regular", 22)
R20 = F("Regular", 20)
SUB = F("Regular", 36)
CHK = F("Regular", 30)

def fh(d, t, f):
    b = d.textbbox((0, 0), t, font=f)
    return b[3] - b[1]

def fw(d, t, f):
    return d.textbbox((0, 0), t, font=f)[2]

def fit_title(d, lines, max_w, start=170):
    """Auto-fit font size biar semua baris muat di max_w."""
    for size in range(start, 79, -2):
        f = F("Bold", size)
        if all(fw(d, ln, f) <= max_w for ln in lines):
            return f
    return F("Bold", 80)

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

# ── Header ──
d.text((S, S), "DMARK", font=B24, fill=WHITE)
page = "1/1"
d.text((W - S - fw(d, page, R22), S), page, font=R22, fill=WHITE)

# ── Footer ── (naik 22px ekstra biar descender gak nyentuh safe zone 20px)
fy = H - S - fh(d, "www.dmark.id", R20) - 22
d.text((S, fy), "www.dmark.id", font=R20, fill=WHITE)
hash_ = "#BrandAwareness #Branding #DigitalMarketing"
d.text((W - S - fw(d, hash_, R20), fy), hash_, font=R20, fill=WHITE)

# ── Konten (vertically middle antara header & footer) ──
title_lines = ["BRAND", "AWARENESS"]
sub_lines = ["Kunci bisnis dikenal,", "dipercaya, dan dipilih"]
items = ["Konsisten di semua platform",
         "Konten edukatif yang relevan",
         "Interaksi & kolaborasi"]

hdr_bot = S + fh(d, "DMARK", B24) + 15
ftr_top = H - S - fh(d, "www.dmark.id", R20) - 35

# ukuran judul pas
max_w = W - 2 * M
J = fit_title(d, title_lines, max_w)

# hitung total tinggi konten
total = 0
for ln in title_lines:
    total += int(fh(d, ln, J) * 1.12)
total += 35 + 10 + 25  # divider gap + height + after
for ln in sub_lines:
    total += int(fh(d, ln, SUB) * 1.5)
total += 25
for it in items:
    total += int(fh(d, it, CHK) * 1.4)

sy = hdr_bot + (ftr_top - hdr_bot - total) // 2
y = sy

for ln in title_lines:
    d.text((M, y), ln, font=J, fill=WHITE)
    y += int(fh(d, ln, J) * 1.12)

# divider putus-putus
y += 15
x = M
while x < M + 300:
    e = min(x + 14, M + 300)
    d.line([(x, y), (e, y)], fill=WHITE, width=3)
    x += 24
y += 15

for ln in sub_lines:
    d.text((M, y), ln, font=SUB, fill=WHITE)
    y += int(fh(d, ln, SUB) * 1.5)
y += 10

for it in items:
    d.text((M, y), "\u2611", font=CHK, fill=WHITE)
    d.text((M + 50, y + 2), it, font=CHK, fill=WHITE)
    y += int(fh(d, it, CHK) * 1.4)

out = os.path.expanduser("~/.openclaw/workspace-atun/dmark_fb_output/dmark_fb_20260816.png")
img.save(out)
print("Saved:", out)
