#!/usr/bin/env python3
"""
Generate single-slide DMark FB post — 2026-08-08
Article: The Skin Code Clinic dan Kekuatan Brand Berbasis Kepercayaan di Industri Kecantikan
Template: DMark BG Orange 1080x1350, bg #F97316, white Poppins, 65px margin, safe zone 20px
"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
BG = "#F97316"
FG = "#FFFFFF"
MARGIN = 65
SAFE = 20

FP = {
    "Bold": "/usr/share/fonts/truetype/poppins/Poppins-Bold.ttf",
    "SemiBold": "/usr/share/fonts/truetype/poppins/Poppins-SemiBold.ttf",
    "Regular": "/usr/share/fonts/truetype/poppins/Poppins-Regular.ttf",
    "Light": "/usr/share/fonts/truetype/poppins/Poppins-Light.ttf",
}
def load_font(size, w="Bold"):
    try:
        return ImageFont.truetype(FP[w], size)
    except Exception:
        return ImageFont.load_default()

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

f_hdr = load_font(24, "Bold")
f_page = load_font(22, "Regular")
f_ftr = load_font(20, "Regular")
f_title = load_font(80, "Bold")
f_sub = load_font(36, "Regular")
f_body = load_font(30, "Regular")
f_quote = load_font(32, "SemiBold")

LS, RS = SAFE, W - SAFE
TS, BS = SAFE, H - SAFE
CL = MARGIN

# Header
d.text((LS, TS), "DMARK", font=f_hdr, fill=FG)
pb = d.textbbox((0,0), "1/1", font=f_page)
d.text((RS-(pb[2]-pb[0]), TS), "1/1", font=f_page, fill=FG)

# Footer — anchor ls (baseline at y=BS-8) so glyphs stay >=20px from bottom
d.text((LS, BS-8), "www.dmark.id", font=f_ftr, fill=FG, anchor="ls")
ht = "#Branding #Kecantikan"
hb = d.textbbox((0,0), ht, font=f_ftr)
d.text((RS-(hb[2]-hb[0]), BS-8), ht, font=f_ftr, fill=FG, anchor="ls")

# Content — vertically centered
title_lines = ["Brand Klinik", "yang Dipercaya", "Bukan Sekadar", "Iklan"]
sub = "Kepercayaan adalah mata uang tertinggi"
quote_lines = ["Brand tidak dibangun di atas iklan,", "melainkan di atas pengalaman dan", "kepercayaan pasien."]
body_lines = [
    "Di tengah industri kecantikan yang ramai janji",
    "manis, The Skin Code Clinic tumbuh lewat",
    "kualitas medis yang konsisten dan diagnosa",
    "menyeluruh sebelum setiap tindakan.",
]

title_lh = d.textbbox((0,0), "Ay", font=f_title)[3]
sub_lh = d.textbbox((0,0), "Ay", font=f_sub)[3]
quote_lh = d.textbbox((0,0), "Ay", font=f_quote)[3]
body_lh = d.textbbox((0,0), "Ay", font=f_body)[3]

g1, g2, g3, g4 = 32, 28, 36, 34
title_h = len(title_lines)*int(title_lh*1.14)
quote_h = len(quote_lines)*int(quote_lh*1.25)
body_h = len(body_lines)*int(body_lh*1.28)
total = title_h + g1 + sub_lh + g2 + quote_h + g3 + body_h

y = MARGIN + ((H-2*MARGIN) - total)//2

for ln in title_lines:
    d.text((CL, y), ln, font=f_title, fill=FG)
    y += int(title_lh*1.14)
y += g1

d.text((CL, y), sub, font=f_sub, fill=FG)
y += sub_lh + g2

# dashed divider
dash, x, end = 28, CL, CL+280
while x < end:
    d.line([(x, y),(x+dash, y)], fill=FG, width=4)
    x += dash+18
y += 30 + g3

for ln in quote_lines:
    d.text((CL, y), ln, font=f_quote, fill=FG)
    y += int(quote_lh*1.25)
y += g4

for ln in body_lines:
    if ln:
        d.text((CL, y), ln, font=f_body, fill=FG)
    y += int(body_lh*1.28)

if y > BS:
    print("WARNING overflow", y, BS)

outdir = "/home/mantapman/.hermes/profiles/atun/workspace/dmark_fb_output"
os.makedirs(outdir, exist_ok=True)
out = os.path.join(outdir, "dmark_fb_20260808.png")
img.save(out)
print("SAVED:", out)
