#!/usr/bin/env python3
"""DMark FB 1-slide carousel — Branding dan Otomasi"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
BG = "#F97316"
TX = "#FFFFFF"
MARGIN = 65
SAFE = 20
FONT_DIR = "/usr/share/fonts/truetype/poppins"

def get_font(name, size):
    path = os.path.join(FONT_DIR, name)
    if os.path.exists(path):
        return ImageFont.truetype(path, size)
    # fallback
    return ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", size)

def wrap_text(draw, text, font, max_width):
    """Wrap text to fit within max_width"""
    words = text.split()
    lines = []
    current = ""
    for w in words:
        test = current + " " + w if current else w
        bb = draw.textbbox((0, 0), test, font=font)
        if bb[2] - bb[0] <= max_width:
            current = test
        else:
            lines.append(current)
            current = w
    if current:
        lines.append(current)
    return lines

def draw_centered_text(draw, lines, font, y_start, max_width, line_gap=10):
    """Draw text vertically centered in available space"""
    line_height = font.size + line_gap
    total_h = len(lines) * line_height
    y = y_start
    for line in lines:
        draw.text((MARGIN, y), line, font=font, fill=TX)
        y += line_height
    return y

def create_slide():
    img = Image.new("RGB", (W, H), BG)
    draw = ImageDraw.Draw(img)

    # Fonts
    font_bold_22 = get_font("Poppins-Bold.ttf", 22)
    font_bold_100 = get_font("Poppins-Bold.ttf", 100)
    font_reg_36 = get_font("Poppins-Regular.ttf", 36)
    font_reg_20 = get_font("Poppins-Regular.ttf", 20)

    # Limit teks area
    max_text_w = W - MARGIN * 2  # 1080 - 130 = 950

    # Header
    draw.text((SAFE, SAFE), "DMARK", font=font_bold_22, fill=TX)

    # Page number (kanan atas)
    page_text = "1"
    pb = draw.textbbox((0, 0), page_text, font=font_bold_22)
    draw.text((W - SAFE - (pb[2] - pb[0]), SAFE), page_text, font=font_bold_22, fill=TX)

    # ---------- CONTENT ----------
    title = "Branding & Otomasi:\nDuet Bisnis 2x Lipat"
    subtitle = "Branding kuat + sistem rapi = pertumbuhan\nbisnis yang nggak bisa lo abaikan."

    # Title - bold besar
    title_lines = wrap_text(draw, title.replace("\n", " "), font_bold_100, max_text_w)
    # Actually handle newlines manually
    title_lines = []
    for part in title.split("\n"):
        title_lines.extend(wrap_text(draw, part, font_bold_100, max_text_w))

    title_h = len(title_lines) * (font_bold_100.size + 8)

    # Subtitle
    sub_lines = wrap_text(draw, subtitle.replace("\n", " "), font_reg_36, max_text_w)
    # handle newline
    sub_lines = []
    for part in subtitle.split("\n"):
        sub_lines.extend(wrap_text(draw, part, font_reg_36, max_text_w))

    sub_h = len(sub_lines) * (font_reg_36.size + 6)

    # Divider (dashed)
    divider_y_offset = 20

    # Hitung total height content
    total_content_h = title_h + divider_y_offset + 10 + sub_h

    # Posisi vertikal tengah
    header_bottom = SAFE + 30  # ~50px from top
    footer_top = H - SAFE - 25  # ~45px from bottom

    available_y = footer_top - header_bottom
    content_start_y = header_bottom + (available_y - total_content_h) // 2

    # Draw title
    y = content_start_y
    for line in title_lines:
        draw.text((MARGIN, y), line, font=font_bold_100, fill=TX)
        y += font_bold_100.size + 8

    y += divider_y_offset

    # Dashed divider
    divider_y = y
    dash_len = 15
    gap = 10
    x = MARGIN
    while x < W - MARGIN:
        x2 = min(x + dash_len, W - MARGIN)
        draw.line([(x, divider_y), (x2, divider_y)], fill=TX, width=3)
        x = x2 + gap

    y += 10

    # Subtitle
    for line in sub_lines:
        draw.text((MARGIN, y), line, font=font_reg_36, fill=TX)
        y += font_reg_36.size + 6

    # Footer
    draw.text((SAFE, H - SAFE - 22), "www.dmark.id", font=font_reg_20, fill=TX)

    hashtag = "#BrandingStrategy #OtomasiBisnis"
    hb = draw.textbbox((0, 0), hashtag, font=font_reg_20)
    draw.text((W - SAFE - (hb[2] - hb[0]), H - SAFE - 22), hashtag, font=font_reg_20, fill=TX)

    out = "/home/mantapman/.hermes/profiles/atun/workspace/dmark_fb_20260729.png"
    img.save(out)
    print(f"Saved: {out}")
    return out

if __name__ == "__main__":
    create_slide()
