#!/usr/bin/env python3
"""
Google Display Ad Generator - Angle B (성장 데이터)
Design Concept: #23 나이키 스타일
Nike-style: pure black, high contrast, powerful typography
"""

import os
import tempfile
from pathlib import Path
from playwright.sync_api import sync_playwright
from PIL import Image

OUTPUT_DIR = Path("/home/jay/workspace/output/google-ads/angle-B/production")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

# ─────────────────────────────────────────────────────────────────────────────
# HTML Templates
# ─────────────────────────────────────────────────────────────────────────────

HTML_1200x628 = """<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;700;900&display=swap" rel="stylesheet">
<style>
  * { margin:0; padding:0; box-sizing:border-box; }
  body { width:1200px; height:628px; overflow:hidden; background:#000; }
  .container {
    position:relative;
    width:1200px;
    height:628px;
    background:#000;
    display:flex;
    flex-direction:column;
    justify-content:center;
    padding:0 80px;
    overflow:hidden;
  }

  /* Subtle diagonal accent line */
  .bg-line {
    position:absolute;
    top:0; right:260px;
    width:3px;
    height:100%;
    background: linear-gradient(to bottom, transparent, #ffffff22, transparent);
  }
  .bg-line2 {
    position:absolute;
    top:0; right:240px;
    width:1px;
    height:100%;
    background: linear-gradient(to bottom, transparent, #ffffff11, transparent);
  }

  /* GROWTH tag */
  .tag {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:22px;
    letter-spacing:8px;
    color:#ffffff44;
    text-transform:uppercase;
    margin-bottom:28px;
  }

  /* Main headline */
  .headline {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:84px;
    line-height:1.0;
    color:#ffffff;
    letter-spacing:-2px;
    margin-bottom:16px;
  }

  /* Accent number – the hero element */
  .accent-number {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:100px;
    line-height:1.0;
    color:#ffffff;
    letter-spacing:-3px;
    margin-bottom:20px;
    /* Slight stroke for depth */
    text-shadow: 0 0 60px rgba(255,255,255,0.15);
  }
  .accent-number .highlight {
    color:#ffffff;
    position:relative;
    display:inline-block;
  }
  /* Underline bar under number */
  .accent-bar {
    width:320px;
    height:6px;
    background:#fff;
    margin-bottom:28px;
  }

  /* Member count */
  .member-count {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:700;
    font-size:64px;
    line-height:1.0;
    color:#ffffffaa;
    letter-spacing:-1px;
    margin-bottom:36px;
  }

  /* Bottom bar */
  .bottom-bar {
    display:flex;
    align-items:center;
    gap:24px;
  }
  .brand {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:40px;
    color:#fff;
    letter-spacing:2px;
  }
  .divider {
    width:2px;
    height:40px;
    background:#ffffff44;
  }
  .cta {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:700;
    font-size:40px;
    color:#fff;
    letter-spacing:1px;
  }

  /* Right-side vertical text decoration */
  .right-deco {
    position:absolute;
    right:60px;
    top:50%;
    transform:translateY(-50%) rotate(90deg);
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:18px;
    letter-spacing:12px;
    color:#ffffff18;
    white-space:nowrap;
    text-transform:uppercase;
  }
</style>
</head>
<body>
<div class="container">
  <div class="bg-line"></div>
  <div class="bg-line2"></div>
  <div class="right-deco">GROWTH DATA 2024</div>

  <div class="tag">GROWTH</div>
  <div class="headline">판을 바꾸는 전략</div>
  <div class="accent-number"><span class="highlight">매출 1,863% 성장</span></div>
  <div class="accent-bar"></div>
  <div class="member-count">482명 → 5,500명</div>
  <div class="bottom-bar">
    <span class="brand">T.O.P 사업단</span>
    <div class="divider"></div>
    <span class="cta">지금 지원하기 →</span>
  </div>
</div>
</body>
</html>"""


HTML_1200x1200 = """<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Noto Sans KR:wght@400;700;900&display=swap" rel="stylesheet">
<style>
  * { margin:0; padding:0; box-sizing:border-box; }
  body { width:1200px; height:1200px; overflow:hidden; background:#000; }
  .container {
    position:relative;
    width:1200px;
    height:1200px;
    background:#000;
    display:flex;
    flex-direction:column;
    align-items:center;
    justify-content:center;
    overflow:hidden;
  }

  /* Horizontal rule decorations */
  .h-line-top {
    position:absolute;
    top:120px; left:80px; right:80px;
    height:1px;
    background: linear-gradient(to right, transparent, #ffffff33, transparent);
  }
  .h-line-bot {
    position:absolute;
    bottom:120px; left:80px; right:80px;
    height:1px;
    background: linear-gradient(to right, transparent, #ffffff33, transparent);
  }

  /* Big corner text */
  .corner-tl {
    position:absolute;
    top:52px; left:80px;
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:20px;
    letter-spacing:8px;
    color:#ffffff28;
    text-transform:uppercase;
  }
  .corner-br {
    position:absolute;
    bottom:52px; right:80px;
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:20px;
    letter-spacing:8px;
    color:#ffffff28;
    text-transform:uppercase;
  }

  /* Content stack – centered */
  .content {
    display:flex;
    flex-direction:column;
    align-items:center;
    text-align:center;
    gap:0;
  }

  .tag {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:26px;
    letter-spacing:12px;
    color:#ffffff44;
    text-transform:uppercase;
    margin-bottom:48px;
  }

  .headline {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:84px;
    line-height:1.1;
    color:#fff;
    letter-spacing:-2px;
    margin-bottom:60px;
  }

  /* Hero number block */
  .hero-block {
    display:flex;
    flex-direction:column;
    align-items:center;
    margin-bottom:60px;
  }
  .hero-label {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:700;
    font-size:36px;
    color:#ffffff66;
    letter-spacing:4px;
    margin-bottom:10px;
  }
  .hero-number {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:140px;
    line-height:0.9;
    color:#ffffff;
    letter-spacing:-6px;
    text-shadow: 0 0 80px rgba(255,255,255,0.12);
  }
  .hero-unit {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:64px;
    color:#ffffff;
    letter-spacing:0;
    margin-top:8px;
  }

  /* Accent bar */
  .accent-bar {
    width:160px;
    height:6px;
    background:#fff;
    margin:0 auto 56px;
  }

  /* Members */
  .members {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:700;
    font-size:64px;
    color:#ffffffaa;
    letter-spacing:-1px;
    margin-bottom:72px;
  }

  /* CTA bar */
  .cta-bar {
    display:flex;
    align-items:center;
    gap:32px;
    padding:28px 60px;
    border:2px solid #ffffff44;
  }
  .brand {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:44px;
    color:#fff;
    letter-spacing:3px;
  }
  .divider {
    width:2px;
    height:44px;
    background:#ffffff44;
  }
  .cta {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:700;
    font-size:44px;
    color:#fff;
  }
</style>
</head>
<body>
<div class="container">
  <div class="h-line-top"></div>
  <div class="h-line-bot"></div>
  <div class="corner-tl">GROWTH</div>
  <div class="corner-br">2024 DATA</div>

  <div class="content">
    <div class="tag">GROWTH</div>
    <div class="headline">판을 바꾸는 전략</div>

    <div class="hero-block">
      <div class="hero-label">매출</div>
      <div class="hero-number">1,863</div>
      <div class="hero-unit">% 성장</div>
    </div>

    <div class="accent-bar"></div>
    <div class="members">482명 → 5,500명</div>

    <div class="cta-bar">
      <span class="brand">T.O.P 사업단</span>
      <div class="divider"></div>
      <span class="cta">지금 지원하기 →</span>
    </div>
  </div>
</div>
</body>
</html>"""


# Render at 600x500 then resize to 300x250
HTML_600x500 = """<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;700;900&display=swap" rel="stylesheet">
<style>
  * { margin:0; padding:0; box-sizing:border-box; }
  body { width:600px; height:500px; overflow:hidden; background:#000; }
  .container {
    position:relative;
    width:600px;
    height:500px;
    background:#000;
    display:flex;
    flex-direction:column;
    align-items:center;
    justify-content:center;
    overflow:hidden;
  }

  /* Top / bottom thin lines */
  .h-line-top {
    position:absolute;
    top:48px; left:36px; right:36px;
    height:1px;
    background: linear-gradient(to right, transparent, #ffffff44, transparent);
  }
  .h-line-bot {
    position:absolute;
    bottom:48px; left:36px; right:36px;
    height:1px;
    background: linear-gradient(to right, transparent, #ffffff44, transparent);
  }

  .tag {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:16px;
    letter-spacing:8px;
    color:#ffffff44;
    text-transform:uppercase;
    margin-bottom:24px;
  }

  /* Hero percent */
  .hero-pct {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:110px;
    line-height:0.9;
    color:#fff;
    letter-spacing:-4px;
    text-shadow: 0 0 60px rgba(255,255,255,0.12);
  }
  .hero-label {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:42px;
    color:#fff;
    letter-spacing:0px;
    margin-top:6px;
    margin-bottom:20px;
  }

  .accent-bar {
    width:80px;
    height:5px;
    background:#fff;
    margin-bottom:24px;
  }

  /* Brand */
  .brand {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:900;
    font-size:32px;
    color:#fff;
    letter-spacing:6px;
    margin-bottom:12px;
  }

  /* Members sub */
  .members {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:700;
    font-size:20px;
    color:#ffffff77;
    letter-spacing:1px;
    margin-bottom:20px;
  }

  .cta {
    font-family: 'Noto Sans KR', sans-serif;
    font-weight:700;
    font-size:20px;
    color:#fff;
    border:1.5px solid #ffffff66;
    padding:10px 28px;
    letter-spacing:1px;
  }
</style>
</head>
<body>
<div class="container">
  <div class="h-line-top"></div>
  <div class="h-line-bot"></div>

  <div class="tag">GROWTH</div>
  <div class="hero-pct">1,863%</div>
  <div class="hero-label">매출 성장</div>
  <div class="accent-bar"></div>
  <div class="brand">T.O.P</div>
  <div class="members">482명 → 5,500명</div>
  <div class="cta">지금 지원하기 →</div>
</div>
</body>
</html>"""


# ─────────────────────────────────────────────────────────────────────────────
# Rendering
# ─────────────────────────────────────────────────────────────────────────────

def render_html_to_png(html_content: str, width: int, height: int, output_path: Path):
    """Render HTML string to PNG using Playwright."""
    with tempfile.NamedTemporaryFile(suffix=".html", mode="w", encoding="utf-8", delete=False) as f:
        f.write(html_content)
        tmp_path = f.name

    try:
        with sync_playwright() as p:
            browser = p.chromium.launch()
            page = browser.new_page(viewport={"width": width, "height": height})
            page.goto(f"file://{tmp_path}")
            # Wait for Google Fonts to load
            page.wait_for_timeout(2500)
            page.screenshot(path=str(output_path), clip={"x": 0, "y": 0, "width": width, "height": height})
            browser.close()
    finally:
        os.unlink(tmp_path)

    print(f"  Saved: {output_path} ({width}x{height})")


def resize_with_pil(src: Path, dst: Path, width: int, height: int):
    """Resize PNG with PIL using high-quality Lanczos."""
    img = Image.open(src)
    img = img.resize((width, height), Image.LANCZOS)
    img.save(dst, "PNG", optimize=True)
    print(f"  Resized → {dst} ({width}x{height})")


# ─────────────────────────────────────────────────────────────────────────────
# Main
# ─────────────────────────────────────────────────────────────────────────────

def main():
    print("\n=== Google Display Ads – Angle B (성장 데이터) ===")
    print("Design: #23 나이키 스타일\n")

    # 1. 1200x628 landscape
    print("[1/3] Rendering 1200x628...")
    render_html_to_png(
        HTML_1200x628,
        width=1200,
        height=628,
        output_path=OUTPUT_DIR / "display-1200x628.png",
    )

    # 2. 1200x1200 square
    print("[2/3] Rendering 1200x1200...")
    render_html_to_png(
        HTML_1200x1200,
        width=1200,
        height=1200,
        output_path=OUTPUT_DIR / "display-1200x1200.png",
    )

    # 3. 300x250 — render at 600x500 then resize
    print("[3/3] Rendering 300x250 (via 600x500)...")
    tmp_600 = OUTPUT_DIR / "_tmp_600x500.png"
    render_html_to_png(
        HTML_600x500,
        width=600,
        height=500,
        output_path=tmp_600,
    )
    resize_with_pil(
        src=tmp_600,
        dst=OUTPUT_DIR / "display-300x250.png",
        width=300,
        height=250,
    )
    tmp_600.unlink()

    print("\nAll ads generated successfully!")
    for f in sorted(OUTPUT_DIR.glob("display-*.png")):
        size = f.stat().st_size // 1024
        print(f"  {f.name}  ({size} KB)")


if __name__ == "__main__":
    main()
