#!/usr/bin/env python3
"""DMark Single Post — 1 slide 1080x1350 BG Orange
Template: bg #F97316, teks putih, Poppins, margin 65px
"""

from PIL import Image, ImageDraw, ImageFont
import os, textwrap

FONT_DIR = os.path.expanduser("~/.fonts")
OUT_DIR = os.path.expanduser("~/.openclaw/workspace-atun/dmark_single")
os.makedirs(OUT_DIR, exist_ok=True)

W, H = 1080, 1350
ORANGE = (249, 115, 22)
WHITE = (255, 255, 255)
M = 65  # margin
CONTENT_W = W - 2 * M  # 950
MAX_Y = H - M - 40  # footer start

# Fonts
BOLD24 = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Bold.ttf"), 24)
REG20 = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Regular.ttf"), 20)
REG22 = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Regular.ttf"), 22)

def draw_header(d, page_label="1/1"):
    d.text((M, M), "DMARK", font=BOLD24, fill=WHITE)
    d.text((W - M, M), page_label, font=REG22, fill=WHITE, anchor="rt")

def draw_footer(d, hashtag=""):
    fy = H - 65
    d.text((M, fy), "www.dmark.id", font=REG20, fill=WHITE)
    if hashtag:
        d.text((W - M, fy), hashtag, font=REG20, fill=WHITE, anchor="rt")

# ============ SLIDE: ARTIKEL ============
# Artikel yang dipilih (dari dmark.id)
article_title = "Sukses dengan Guerilla Marketing: Studi Kasus Brand yang Berhasil Naik Kelas"
article_url = "https://www.dmark.id/sukses-guerilla-marketing-studi-kasus-brand/"
hashtag = "#GuerillaMarketing #Branding #PemasaranKreatif"

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

draw_header(d, "1/1")

# --- JUDUL ---
# Try different font sizes based on length
title_y = 200
if len(article_title) <= 40:
    title_font = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Bold.ttf"), 120)
elif len(article_title) <= 60:
    title_font = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Bold.ttf"), 100)
elif len(article_title) <= 80:
    title_font = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Bold.ttf"), 80)
else:
    title_font = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Bold.ttf"), 72)

# Word wrap the title
words = article_title.split()
lines = []
current_line = ""
for word in words:
    test = current_line + " " + word if current_line else word
    bbox = d.textbbox((0, 0), test, font=title_font)
    w = bbox[2] - bbox[0]
    if w < CONTENT_W - 20:
        current_line = test
    else:
        if current_line:
            lines.append(current_line)
        current_line = word
if current_line:
    lines.append(current_line)

# Draw title lines
ty = title_y
for line in lines:
    d.text((M, ty), line, font=title_font, fill=WHITE)
    ty += int((title_font.getbbox(line)[3] - title_font.getbbox(line)[1]) * 1.25)

# --- DIVIDER ---
divider_y = ty + 30
# draw dashed divider
dash_len, gap = 14, 10
x = M
while x < M + 300:
    xe = min(x + dash_len, M + 300)
    d.line([(x, divider_y), (xe, divider_y)], fill=WHITE, width=2)
    x += dash_len + gap

# --- DESKRIPSI ---
desc = "Pelajari bagaimana brand-brand sukses menggunakan strategi guerilla marketing untuk naik kelas — tanpa budget besar, tapi dengan kreativitas maksimal."
sub_font = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Regular.ttf"), 36)

desc_words = desc.split()
desc_lines = []
current_line = ""
for word in desc_words:
    test = current_line + " " + word if current_line else word
    bbox = d.textbbox((0, 0), test, font=sub_font)
    w = bbox[2] - bbox[0]
    if w < CONTENT_W - 20:
        current_line = test
    else:
        if current_line:
            desc_lines.append(current_line)
        current_line = word
if current_line:
    desc_lines.append(current_line)

desc_y = divider_y + 50
for line in desc_lines:
    d.text((M, desc_y), line, font=sub_font, fill=WHITE)
    desc_y += int((sub_font.getbbox(line)[3] - sub_font.getbbox(line)[1]) * 1.55)

# --- CTA LINK ---
cta_font = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Bold.ttf"), 30)
cta_y = desc_y + 60
d.text((M, cta_y), "Baca selengkapnya di:", font=cta_font, fill=WHITE)
link_font = ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Regular.ttf"), 24)
link_y = cta_y + 40
d.text((M, link_y), article_url, font=link_font, fill=WHITE)

draw_footer(d, hashtag)

out_path = os.path.join(OUT_DIR, "dmark_fb_post.png")
img.save(out_path)
print(f"✅ Slide saved: {out_path}")
