#!/usr/bin/env python3
"""
Create Yasma Natura featured image: Kolagen vs Glutathione
Ukuran: 1000x567 WebP — buat featured image artikel WordPress
"""
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageEnhance
import os

W, H = 1000, 567
MARGIN = 30
MAX_W = W - MARGIN * 2  # ~940px

ORANGE = (255, 106, 0, 255)
WHITE = (255, 255, 255, 255)
DARK_OVERLAY = (0, 0, 0, 80)

FONT_DIR = "/usr/share/fonts/truetype"
try:
    FONT_TITLE = ImageFont.truetype(f"{FONT_DIR}/dejavu/DejaVuSans-Bold.ttf", 48)
    FONT_SUB = ImageFont.truetype(f"{FONT_DIR}/dejavu/DejaVuSans.ttf", 22)
    FONT_BRAND = ImageFont.truetype(f"{FONT_DIR}/dejavu/DejaVuSans-Bold.ttf", 18)
except:
    FONT_TITLE = ImageFont.load_default()
    FONT_SUB = ImageFont.load_default()
    FONT_BRAND = ImageFont.load_default()

def load_and_prep_bg(img_path):
    img = Image.open(img_path).convert("RGBA")
    # Center crop to 1000x567
    src_w, src_h = img.size
    target_ratio = W / H
    src_ratio = src_w / src_h
    
    if src_ratio > target_ratio:
        new_h = src_h
        new_w = int(src_h * target_ratio)
        left = (src_w - new_w) // 2
        top = 0
    else:
        new_w = src_w
        new_h = int(src_w / target_ratio)
        left = 0
        top = (src_h - new_h) // 2
    
    img = img.crop((left, top, left + new_w, top + new_h))
    img = img.resize((W, H), Image.LANCZOS)
    
    # Dark overlay for readability
    overlay = Image.new("RGBA", (W, H), DARK_OVERLAY)
    img = Image.alpha_composite(img, overlay)
    
    return img

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

def create_featured():
    img = load_and_prep_bg("pexels_raw.jpg")
    draw = ImageDraw.Draw(img)
    
    # Brand bar (top)
    draw.text((MARGIN, MARGIN), "YASMA NATURA", font=FONT_BRAND, fill=ORANGE)
    
    # Center content
    center_y = H // 2 - 10
    
    # Title
    title = "Kolagen vs Glutathione"
    lines = wrap_text(draw, title, FONT_TITLE, MAX_W)
    line_h = FONT_TITLE.getbbox("A")[3] - FONT_TITLE.getbbox("A")[1] + 8
    total_title_h = len(lines) * line_h
    
    # Subtitle
    subtitle = "Mana yang Lebih Cocok untuk Brand Kecantikan Anda?"
    sub_lines = wrap_text(draw, subtitle, FONT_SUB, MAX_W)
    sub_h = FONT_SUB.getbbox("A")[3] - FONT_SUB.getbbox("A")[1] + 6
    total_sub_h = len(sub_lines) * sub_h
    
    total_h = total_title_h + 10 + total_sub_h
    start_y = center_y - total_h // 2
    
    # Draw title
    y = start_y
    for line in lines:
        bbox = draw.textbbox((0, 0), line, font=FONT_TITLE)
        # Center text horizontally
        x = (W - (bbox[2] - bbox[0])) // 2
        draw.text((x, y), line, font=FONT_TITLE, fill=WHITE)
        y += line_h
    
    # Divider (centered)
    y += 5
    div_start = (W - 200) // 2
    for i in range(7):
        dx = div_start + i * 30
        draw.line([(dx, y), (dx + 18, y)], fill=ORANGE, width=2)
    
    y += 10
    
    # Draw subtitle
    for line in sub_lines:
        bbox = draw.textbbox((0, 0), line, font=FONT_SUB)
        x = (W - (bbox[2] - bbox[0])) // 2
        draw.text((x, y), line, font=FONT_SUB, fill=WHITE)
        y += sub_h
    
    # Bottom brand
    draw.text((W - MARGIN - 150, H - MARGIN - 10), "www.yasmanatura.co.id", font=FONT_BRAND, fill=ORANGE)
    
    return img

def main():
    os.makedirs("output", exist_ok=True)
    print("🎨 Creating Yasma Natura featured image (1000x567)...")
    img = create_featured()
    out = "output/yasma_kolagen_vs_glutathione_featured.webp"
    img.save(out, "WEBP", quality=92, method=6)
    print(f"  ✅ {out}")
    print("🎉 Done!")

if __name__ == "__main__":
    main()
