#!/usr/bin/env python3
"""
Generate single-slide DMark BG Orange carousel image
Template: 1080x1350, bg #F97316, white Poppins text, 65px margins
"""
from PIL import Image, ImageDraw, ImageFont
import os
import sys

# Canvas
W, H = 1080, 1350
BG_COLOR = "#F97316"
TEXT_COLOR = "#FFFFFF"
MARGIN = 65
SAFE = 20

# Fonts - try to find Poppins
def find_font():
    paths = [
        "/usr/share/fonts/truetype/poppins/Poppins-Bold.ttf",
        "/usr/share/fonts/truetype/poppins/Poppins-SemiBold.ttf",
        "/usr/share/fonts/truetype/poppins/Poppins-Regular.ttf",
        "/usr/share/fonts/truetype/poppins/Poppins-Light.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
    ]
    for p in paths:
        if os.path.exists(p):
            return p
    return None

font_path = find_font()
if not font_path:
    print("No font found, installing poppins...")
    os.system("apt-get update && apt-get install -y fonts-poppins 2>/dev/null || true")
    font_path = "/usr/share/fonts/truetype/poppins/Poppins-Bold.ttf"

# Create image
img = Image.new("RGB", (W, H), BG_COLOR)
draw = ImageDraw.Draw(img)

# Load fonts
def load_font(size, weight="Bold"):
    try:
        if weight == "Bold":
            fp = "/usr/share/fonts/truetype/poppins/Poppins-Bold.ttf"
        elif weight == "SemiBold":
            fp = "/usr/share/fonts/truetype/poppins/Poppins-SemiBold.ttf"
        elif weight == "Regular":
            fp = "/usr/share/fonts/truetype/poppins/Poppins-Regular.ttf"
        elif weight == "Light":
            fp = "/usr/share/fonts/truetype/poppins/Poppins-Light.ttf"
        else:
            fp = font_path
        return ImageFont.truetype(fp, size)
    except:
        return ImageFont.load_default()

font_header = load_font(24, "Bold")
font_page = load_font(22, "Regular")
font_title = load_font(170, "Bold")
font_subtitle = load_font(36, "Regular")
font_checklist = load_font(30, "Regular")
font_footer = load_font(20, "Regular")

# Safe zone boundaries
left_safe = SAFE
right_safe = W - SAFE
top_safe = SAFE
bottom_safe = H - SAFE

# Content area (65px margins)
content_left = MARGIN
content_right = W - MARGIN
content_top = MARGIN
content_bottom = H - MARGIN
content_width = content_right - content_left

# ============ HEADER ============
# "DMARK" top-left
draw.text((left_safe, top_safe), "DMARK", font=font_header, fill=TEXT_COLOR)

# Page number "1/1" top-right
page_text = "1/1"
page_bbox = draw.textbbox((0, 0), page_text, font=font_page)
page_w = page_bbox[2] - page_bbox[0]
draw.text((right_safe - page_w, top_safe), page_text, font=font_page, fill=TEXT_COLOR)

# ============ FOOTER ============
# www.dmark.id bottom-left
footer_left = "www.dmark.id"
draw.text((left_safe, bottom_safe - 20), footer_left, font=font_footer, fill=TEXT_COLOR)

# Hashtag bottom-right
hashtag = "#Branding #DigitalMarketing"
hashtag_bbox = draw.textbbox((0, 0), hashtag, font=font_footer)
hashtag_w = hashtag_bbox[2] - hashtag_bbox[0]
draw.text((right_safe - hashtag_w, bottom_safe - 20), hashtag, font=font_footer, fill=TEXT_COLOR)

# ============ CONTENT (Vertically Centered) ============
# Title: "Sukses dengan\nGuerilla Marketing"
title_lines = ["Sukses dengan", "Guerilla Marketing"]

# Calculate total content height
line_height_title = font_title.getbbox("Ay")[3] - font_title.getbbox("Ay")[1]  # approx
title_h = len(title_lines) * line_height_title * 1.15

# Divider (dashed line)
divider_h = 20

# Subtitle
subtitle = "Studi Kasus Brand yang Berhasil Naik Kelas"
subtitle_bbox = draw.textbbox((0, 0), subtitle, font=font_subtitle)
subtitle_w = subtitle_bbox[2] - subtitle_bbox[0]
subtitle_h = subtitle_bbox[3] - subtitle_bbox[1]

# Checklist item
checklist = "☑ Implementasi kreatif tanpa budget besar"
checklist_bbox = draw.textbbox((0, 0), checklist, font=font_checklist)
checklist_w = checklist_bbox[2] - checklist_bbox[0]
checklist_h = checklist_bbox[3] - checklist_bbox[1]

total_content_h = title_h + divider_h + subtitle_h + 30 + checklist_h

# Starting Y (vertically centered in content area)
content_area_h = content_bottom - content_top
start_y = content_top + (content_area_h - total_content_h) // 2

y = start_y

# Draw title lines
for i, line in enumerate(title_lines):
    bbox = draw.textbbox((0, 0), line, font=font_title)
    line_w = bbox[2] - bbox[0]
    # Left align
    draw.text((content_left, y), line, font=font_title, fill=TEXT_COLOR)
    y += int(line_height_title * 1.15)

# Divider (dashed line)
divider_y = y + 10
dash_len = 15
gap_len = 10
x = content_left
while x < content_left + content_width:
    end_x = min(x + dash_len, content_left + content_width)
    draw.line([(x, divider_y), (end_x, divider_y)], fill=TEXT_COLOR, width=3)
    x += dash_len + gap_len
y = divider_y + 15

# Subtitle (wrapped if needed)
# Check if subtitle fits
if subtitle_w > content_width:
    # Simple wrap - split at space near middle
    words = subtitle.split()
    mid = len(words) // 2
    line1 = " ".join(words[:mid])
    line2 = " ".join(words[mid:])
    for line in [line1, line2]:
        bbox = draw.textbbox((0, 0), line, font=font_subtitle)
        lw = bbox[2] - bbox[0]
        draw.text((content_left, y), line, font=font_subtitle, fill=TEXT_COLOR)
        y += subtitle_h + 8
else:
    draw.text((content_left, y), subtitle, font=font_subtitle, fill=TEXT_COLOR)
    y += subtitle_h + 30

# Checklist
draw.text((content_left, y), checklist, font=font_checklist, fill=TEXT_COLOR)

# Save
output_path = "/home/mantapman/.hermes/profiles/atun/workspace/dmark_fb_orange_single.png"
img.save(output_path, "PNG")
print(f"Saved: {output_path}")
