#!/usr/bin/env python3
"""Generate featured image for Apotek Arek Lancor article"""
from PIL import Image, ImageDraw, ImageFont
import os, re, textwrap

OUTPUT = "/home/mantapman/.hermes/profiles/atun/workspace/apotek_4352_featured.png"
W, H = 1000, 567

# Colors
BG = (0, 102, 71)       # Dark green — pharmacy/apotek vibe
WHITE = (255, 255, 255)
LIGHT_GREEN = (144, 190, 109)
DARK = (0, 60, 40)
RED = (220, 50, 50)
ORANGE = (240, 150, 50)
YELLOW = (255, 205, 40)

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

# Try to find fonts
font_dir = "/usr/share/fonts"
bold_paths = [
    "/usr/share/fonts/truetype/poppins/Poppins-Bold.ttf",
    "/usr/share/fonts/truetype/montserrat/Montserrat-Bold.ttf",
    "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
]
reg_paths = [
    "/usr/share/fonts/truetype/poppins/Poppins-Regular.ttf",
    "/usr/share/fonts/truetype/montserrat/Montserrat-Regular.ttf",
    "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
]

def find_font(paths, size):
    for p in paths:
        if os.path.exists(p):
            return ImageFont.truetype(p, size)
    return ImageFont.load_default()

font_bold = find_font(bold_paths, 58)
font_bold_sm = find_font(bold_paths, 36)
font_reg = find_font(reg_paths, 22)
font_reg_sm = find_font(reg_paths, 18)

# Background shape — subtle circle decoration
draw.ellipse([(-50, 50), (350, 450)], fill=(0, 130, 90), outline=None)
draw.ellipse([(600, -80), (950, 270)], fill=(0, 130, 90), outline=None)
draw.ellipse([(750, 300), (1100, 600)], fill=(0, 120, 85), outline=None)

# Red warning circle — peringatan
rx, ry = 780, 350
r = 80
draw.ellipse([(rx-r, ry-r), (rx+r, ry+r)], fill=RED, outline=WHITE, width=4)
draw.text((rx, ry-8), "!", fill=WHITE, font=find_font(bold_paths, 90), anchor="mm")
draw.text((rx-30, ry+50), "AWAS", fill=WHITE, font=find_font(bold_paths, 22), anchor="mm")
draw.text((rx+30, ry+50), "BAHAYA", fill=WHITE, font=find_font(bold_paths, 22), anchor="mm")

# Pharmacy cross symbol (top right)
cross_cx, cross_cy = 880, 100
cs = 30
draw.rectangle([(cross_cx-10, cross_cy-cs), (cross_cx+10, cross_cy+cs)], fill=LIGHT_GREEN)
draw.rectangle([(cross_cx-cs, cross_cy-10), (cross_cx+cs, cross_cy+10)], fill=LIGHT_GREEN)
draw.rectangle([(cross_cx-4, cross_cy-cs+4), (cross_cx+4, cross_cy+cs-4)], fill=WHITE)
draw.rectangle([(cross_cx-cs+4, cross_cy-4), (cross_cx+cs-4, cross_cy+4)], fill=WHITE)

# Pill capsule illustration (left)
# Capsule body
from PIL import ImageDraw as ID
import math

# Draw a few pills scattered
pill_colors = [(255, 100, 100), (100, 150, 255), (255, 200, 50), (100, 200, 100)]
pill_positions = [(80, 350, 30), (150, 430, 20), (50, 300, 25), (200, 380, 22)]

for px, py, angle in pill_positions:
    # Rotated pill - simplified as just an oval + rect
    pw, ph = 50, 20
    draw.rounded_rectangle([(px-ph//2, py-pw//2), (px+ph//2, py+pw//2)], radius=10, fill=pill_colors[pill_positions.index((px,py,angle))])

# Additional pills with different rotation
draw.rounded_rectangle([(60, 260), (110, 285)], radius=12, fill=(200, 80, 80))
draw.rounded_rectangle([(180, 310), (225, 330)], radius=10, fill=(80, 150, 220))

# Title
title = "BAHAYA ANTIBIOTIK"
title2 = "TANPA RESEP DOKTER"

draw.text((40, 40), title, fill=WHITE, font=find_font(bold_paths, 68), anchor="lm")
draw.text((40, 115), title2, fill=YELLOW, font=find_font(bold_paths, 58), anchor="lm")

# Accent line
draw.rectangle([(40, 150), (450, 156)], fill=LIGHT_GREEN)

# Tagline
draw.text((40, 480), "www.apotekpamekasan.com", fill=LIGHT_GREEN, font=find_font(reg_paths, 20), anchor="lm")

# Warning text
draw.text((40, 200), "Konsumsi antibiotik sembarangan bisa berakibat fatal!", fill=WHITE, font=find_font(reg_paths, 22), anchor="lm")
draw.text((40, 240), "Hanya dengan resep dokter — untuk keselamatan Anda", fill=WHITE, font=find_font(reg_paths, 18), anchor="lm")

# Pill row at bottom decorative
for i in range(6):
    px = 520 + i * 75
    py = 450
    colors = [(200,80,80), (80,150,220), (200,170,50), (100,200,100), (200,80,80), (80,150,220)]
    draw.rounded_rectangle([(px, py), (px+55, py+22)], radius=11, fill=colors[i])

img.save(OUTPUT, "PNG")
print(f"✅ Saved: {OUTPUT}")
print(f"   Size: {img.size}")
