#!/usr/bin/env python3
"""
Concept #43 — Income Proof Typography
PENNY "Price Packs" D&AD 2025 Yellow Pencil inspired
1080x1080px PNG, deep coral background, pure typographic power
"""

from PIL import Image, ImageDraw, ImageFont
import shutil

# ── Canvas ─────────────────────────────────────────────────────────────────
W, H = 1080, 1080
BG_COLOR   = (232, 96, 76)        # #E8604C  deep coral

# Text colours (RGBA)
WHITE_FULL   = (255, 255, 255, 255)
WHITE_85     = (255, 255, 255, 216)   # 85 %  ≈ 217
WHITE_70     = (255, 255, 255, 178)   # 70 %  ≈ 179

# ── Font paths ──────────────────────────────────────────────────────────────
FONT_DIR = "/home/jay/.local/share/fonts/Pretendard"

F_REGULAR    = f"{FONT_DIR}/Pretendard-Regular.otf"
F_EXTRABOLD  = f"{FONT_DIR}/Pretendard-ExtraBold.otf"
F_BLACK      = f"{FONT_DIR}/Pretendard-Black.otf"
F_MEDIUM     = f"{FONT_DIR}/Pretendard-Medium.otf"

# ── Copy lines ──────────────────────────────────────────────────────────────
TEXT_TOP    = "신입 정착지원금"
TEXT_MAIN   = "1,000만원"
TEXT_BTM    = "T.O.P 사업단  |  지금 상담 신청하기"

# ── Helper: draw text with alpha compositing ────────────────────────────────
def draw_text_rgba(base_img: Image.Image, text: str, font: ImageFont.FreeTypeFont,
                   x: int, y: int, color_rgba: tuple):
    """Stamp RGBA-coloured text onto an RGB canvas via a temp RGBA layer."""
    overlay = Image.new("RGBA", base_img.size, (0, 0, 0, 0))
    d = ImageDraw.Draw(overlay)
    d.text((x, y), text, font=font, fill=color_rgba)
    base_img.paste(Image.alpha_composite(base_img.convert("RGBA"), overlay).convert("RGB"))


def get_text_size(text: str, font: ImageFont.FreeTypeFont):
    """Return (width, height) of rendered text using getbbox."""
    bbox = font.getbbox(text)       # (left, top, right, bottom)
    w = bbox[2] - bbox[0]
    h = bbox[3] - bbox[1]
    return w, h, bbox[1]            # width, height, top-offset (ascent gap)


# ── Auto-fit main font size ──────────────────────────────────────────────────
MAX_MAIN_W = int(W * 0.92)         # 92 % of canvas width
font_size_main = 220               # start large, step down until fits

while font_size_main > 100:
    fnt_main = ImageFont.truetype(F_BLACK, font_size_main)
    w, h, _ = get_text_size(TEXT_MAIN, fnt_main)
    if w <= MAX_MAIN_W:
        break
    font_size_main -= 4

# ── Load remaining fonts ────────────────────────────────────────────────────
fnt_top  = ImageFont.truetype(F_REGULAR,   64)
fnt_btm  = ImageFont.truetype(F_MEDIUM,    40)

# ── Measure all three lines ─────────────────────────────────────────────────
tw_top,  th_top,  to_top  = get_text_size(TEXT_TOP,  fnt_top)
tw_main, th_main, to_main = get_text_size(TEXT_MAIN, fnt_main)
tw_btm,  th_btm,  to_btm  = get_text_size(TEXT_BTM,  fnt_btm)

# ── Vertical layout ─────────────────────────────────────────────────────────
# Gap between sub → main: 28px; main → CTA: 64px
GAP_TOP_MAIN = 28
GAP_MAIN_BTM = 60

total_h = th_top + GAP_TOP_MAIN + th_main + GAP_MAIN_BTM + th_btm
start_y = (H - total_h) // 2 - 40  # optical centre correction

y_top  = start_y
y_main = y_top  + th_top  + GAP_TOP_MAIN
y_btm  = y_main + th_main + GAP_MAIN_BTM

# Horizontal centres
x_top  = (W - tw_top)  // 2
x_main = (W - tw_main) // 2
x_btm  = (W - tw_btm)  // 2

# ── Build image ──────────────────────────────────────────────────────────────
img = Image.new("RGB", (W, H), BG_COLOR)

# Sub-text  (rgba 85 %)
draw_text_rgba(img, TEXT_TOP,  fnt_top,  x_top,  y_top  - to_top,  WHITE_85)
# Main number (pure white)
draw_text_rgba(img, TEXT_MAIN, fnt_main, x_main, y_main - to_main, WHITE_FULL)
# CTA text (rgba 70 %)
draw_text_rgba(img, TEXT_BTM,  fnt_btm,  x_btm,  y_btm  - to_btm,  WHITE_70)

# ── Save ─────────────────────────────────────────────────────────────────────
OUT_PRIMARY = "/home/jay/workspace/output/meta-ads/concept-catalog/43-dad-price-proof/sample.png"
OUT_COPY    = "/home/jay/workspace/output/meta-ads/concept-catalog/43-dad-price-proof/43-income-proof-typography.png"

img.save(OUT_PRIMARY, "PNG", optimize=False)
shutil.copy2(OUT_PRIMARY, OUT_COPY)

print(f"✓ saved → {OUT_PRIMARY}")
print(f"✓ copied → {OUT_COPY}")
print(f"  canvas   : {W}×{H}px")
print(f"  bg color : #E8604C (deep coral)")
print(f"  main font: Pretendard-Black @ {font_size_main}px")
print(f"  main text width: {tw_main}px / canvas {W}px  ({tw_main/W*100:.1f}%)")
