#!/usr/bin/env python3
"""
Google Display Ad renderer for Angle E (DB+코칭 지원)
Design Concept: #49 Spikes 신뢰/보호 - Trust/protection theme
Uses HTML + Playwright rendering (hybrid-image approach)
"""

import asyncio
import os
from pathlib import Path
from playwright.async_api import async_playwright
from PIL import Image

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

# ─── HTML templates ──────────────────────────────────────────────────────────

HTML_1200x628 = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@300;400;500;700;900&display=swap" rel="stylesheet">
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  body {
    width: 1200px;
    height: 628px;
    overflow: hidden;
    font-family: 'Noto Sans KR', sans-serif;
    background: #0A1628;
  }
  .ad-container {
    position: relative;
    width: 1200px;
    height: 628px;
    background: linear-gradient(135deg, #0A1628 0%, #0F2040 45%, #0D1E35 70%, #0A1628 100%);
    display: flex;
    align-items: center;
    overflow: hidden;
  }

  /* Warm glow overlay */
  .warm-glow {
    position: absolute;
    top: -100px;
    left: -100px;
    width: 600px;
    height: 600px;
    background: radial-gradient(circle, rgba(255,193,7,0.06) 0%, transparent 70%);
    pointer-events: none;
  }
  .warm-glow-right {
    position: absolute;
    bottom: -150px;
    right: -100px;
    width: 700px;
    height: 700px;
    background: radial-gradient(circle, rgba(76,175,80,0.07) 0%, transparent 65%);
    pointer-events: none;
  }

  /* Shield/protection motif - decorative arc */
  .shield-arc {
    position: absolute;
    right: 60px;
    top: 50%;
    transform: translateY(-50%);
    width: 320px;
    height: 320px;
    border: 2px solid rgba(76,175,80,0.18);
    border-radius: 50%;
  }
  .shield-arc::before {
    content: '';
    position: absolute;
    top: 16px; left: 16px; right: 16px; bottom: 16px;
    border: 1.5px solid rgba(76,175,80,0.10);
    border-radius: 50%;
  }
  .shield-arc::after {
    content: '';
    position: absolute;
    top: 36px; left: 36px; right: 36px; bottom: 36px;
    border: 1px solid rgba(255,193,7,0.08);
    border-radius: 50%;
  }

  /* Subtle grid lines */
  .grid-lines {
    position: absolute;
    inset: 0;
    background-image:
      linear-gradient(rgba(76,175,80,0.04) 1px, transparent 1px),
      linear-gradient(90deg, rgba(76,175,80,0.04) 1px, transparent 1px);
    background-size: 60px 60px;
  }

  /* Left content */
  .content-left {
    position: relative;
    z-index: 10;
    padding: 0 0 0 64px;
    flex: 0 0 auto;
    width: 700px;
  }

  .tag {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: rgba(255,193,7,0.12);
    border: 1px solid rgba(255,193,7,0.35);
    border-radius: 24px;
    padding: 8px 20px;
    margin-bottom: 24px;
  }
  .tag-dot {
    width: 8px;
    height: 8px;
    background: #FFC107;
    border-radius: 50%;
    box-shadow: 0 0 8px rgba(255,193,7,0.7);
  }
  .tag-text {
    font-size: 24px;
    font-weight: 600;
    color: #FFC107;
    letter-spacing: 0.04em;
  }

  .headline {
    font-size: 88px;
    font-weight: 900;
    color: #FFFFFF;
    line-height: 1.05;
    letter-spacing: -0.025em;
    margin-bottom: 16px;
    white-space: nowrap;
  }

  .divider {
    width: 60px;
    height: 3px;
    background: linear-gradient(90deg, #4CAF50, transparent);
    margin-bottom: 20px;
  }

  .sub-text {
    font-size: 52px;
    font-weight: 500;
    color: rgba(255,255,255,0.75);
    line-height: 1.25;
    letter-spacing: -0.01em;
    margin-bottom: 32px;
    white-space: nowrap;
  }

  /* Bottom CTA */
  .cta-bar {
    display: inline-flex;
    align-items: center;
    gap: 14px;
    background: rgba(76,175,80,0.15);
    border: 1px solid rgba(76,175,80,0.4);
    border-radius: 8px;
    padding: 12px 24px;
  }
  .cta-text {
    font-size: 36px;
    font-weight: 700;
    color: #4CAF50;
    letter-spacing: 0.01em;
    white-space: nowrap;
  }
  .cta-arrow {
    font-size: 32px;
    color: #4CAF50;
  }

  /* Right content - accent */
  .content-right {
    position: relative;
    z-index: 10;
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-right: 50px;
  }

  .accent-number {
    font-size: 96px;
    font-weight: 900;
    color: #4CAF50;
    line-height: 1.0;
    letter-spacing: -0.03em;
    text-align: center;
    text-shadow: 0 0 40px rgba(76,175,80,0.4);
    margin-bottom: 6px;
    white-space: nowrap;
  }
  .accent-label {
    font-size: 40px;
    font-weight: 600;
    color: rgba(255,255,255,0.6);
    text-align: center;
    letter-spacing: 0.02em;
    white-space: nowrap;
  }

  /* Vertical separator */
  .separator {
    position: absolute;
    left: 700px;
    top: 60px;
    bottom: 60px;
    width: 1px;
    background: linear-gradient(180deg, transparent, rgba(76,175,80,0.25) 30%, rgba(76,175,80,0.25) 70%, transparent);
    z-index: 5;
  }
</style>
</head>
<body>
<div class="ad-container">
  <div class="grid-lines"></div>
  <div class="warm-glow"></div>
  <div class="warm-glow-right"></div>
  <div class="shield-arc"></div>
  <div class="separator"></div>

  <div class="content-left">
    <div class="tag">
      <div class="tag-dot"></div>
      <span class="tag-text">T.O.P 사업단</span>
    </div>
    <div class="headline">혼자가 아닙니다</div>
    <div class="divider"></div>
    <div class="sub-text">1:1 밀착 코칭 시스템</div>
    <div class="cta-bar">
      <span class="cta-text">T.O.P 사업단 | 지금 시작하기</span>
      <span class="cta-arrow">→</span>
    </div>
  </div>

  <div class="content-right">
    <div class="accent-number">30여종</div>
    <div class="accent-label">무상 DB 지원</div>
  </div>
</div>
</body>
</html>"""

HTML_1200x1200 = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@300;400;500;700;900&display=swap" rel="stylesheet">
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  body {
    width: 1200px;
    height: 1200px;
    overflow: hidden;
    font-family: 'Noto Sans KR', sans-serif;
    background: #0A1628;
  }
  .ad-container {
    position: relative;
    width: 1200px;
    height: 1200px;
    background: linear-gradient(160deg, #0A1628 0%, #0F2240 35%, #0B1A2E 65%, #0A1628 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 64px 80px 56px;
    overflow: hidden;
  }

  /* Background decorative circles - shield motif */
  .circle-bg {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 860px;
    height: 860px;
    border: 1.5px solid rgba(76,175,80,0.10);
    border-radius: 50%;
    pointer-events: none;
  }
  .circle-bg-2 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 660px;
    height: 660px;
    border: 1px solid rgba(76,175,80,0.07);
    border-radius: 50%;
    pointer-events: none;
  }
  .circle-bg-3 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 1020px;
    height: 1020px;
    border: 1px solid rgba(255,193,7,0.06);
    border-radius: 50%;
    pointer-events: none;
  }

  /* Warm glow */
  .warm-glow {
    position: absolute;
    bottom: -200px;
    left: 50%;
    transform: translateX(-50%);
    width: 900px;
    height: 600px;
    background: radial-gradient(ellipse, rgba(76,175,80,0.08) 0%, transparent 65%);
    pointer-events: none;
  }
  .gold-glow {
    position: absolute;
    top: -100px;
    left: 50%;
    transform: translateX(-50%);
    width: 700px;
    height: 500px;
    background: radial-gradient(ellipse, rgba(255,193,7,0.06) 0%, transparent 65%);
    pointer-events: none;
  }

  /* Subtle grid */
  .grid-lines {
    position: absolute;
    inset: 0;
    background-image:
      linear-gradient(rgba(76,175,80,0.035) 1px, transparent 1px),
      linear-gradient(90deg, rgba(76,175,80,0.035) 1px, transparent 1px);
    background-size: 80px 80px;
  }

  /* Top section */
  .top-section {
    position: relative;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
  }

  .tag {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    background: rgba(255,193,7,0.10);
    border: 1px solid rgba(255,193,7,0.35);
    border-radius: 28px;
    padding: 10px 28px;
  }
  .tag-dot {
    width: 10px;
    height: 10px;
    background: #FFC107;
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(255,193,7,0.7);
  }
  .tag-text {
    font-size: 32px;
    font-weight: 600;
    color: #FFC107;
    letter-spacing: 0.05em;
  }

  .headline {
    font-size: 92px;
    font-weight: 900;
    color: #FFFFFF;
    text-align: center;
    letter-spacing: -0.025em;
    line-height: 1.1;
  }

  /* Middle accent section */
  .middle-section {
    position: relative;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
  }

  .accent-badge {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(76,175,80,0.08);
    border: 2px solid rgba(76,175,80,0.35);
    border-radius: 20px;
    padding: 32px 64px;
    margin-bottom: 8px;
  }
  .accent-number {
    font-size: 96px;
    font-weight: 900;
    color: #4CAF50;
    line-height: 1.0;
    letter-spacing: -0.03em;
    text-shadow: 0 0 50px rgba(76,175,80,0.5);
  }
  .accent-sub {
    font-size: 48px;
    font-weight: 700;
    color: #4CAF50;
    letter-spacing: 0.03em;
    margin-top: 4px;
  }

  .sub-text {
    font-size: 64px;
    font-weight: 500;
    color: rgba(255,255,255,0.78);
    text-align: center;
    letter-spacing: -0.01em;
  }

  /* Bullet points section */
  .bullets-section {
    position: relative;
    z-index: 10;
    display: flex;
    flex-direction: column;
    gap: 18px;
    width: 100%;
    max-width: 860px;
  }

  .bullet-item {
    display: flex;
    align-items: center;
    gap: 16px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    padding: 18px 32px;
  }
  .bullet-icon {
    width: 14px;
    height: 14px;
    background: #4CAF50;
    border-radius: 50%;
    flex-shrink: 0;
    box-shadow: 0 0 8px rgba(76,175,80,0.6);
  }
  .bullet-text {
    font-size: 42px;
    font-weight: 500;
    color: rgba(255,255,255,0.82);
    letter-spacing: -0.01em;
  }

  /* Bottom CTA */
  .bottom-section {
    position: relative;
    z-index: 10;
    width: 100%;
    display: flex;
    justify-content: center;
  }
  .cta-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    background: linear-gradient(135deg, #4CAF50, #45a049);
    border-radius: 12px;
    padding: 22px 60px;
    width: 100%;
    max-width: 860px;
    box-shadow: 0 8px 40px rgba(76,175,80,0.3);
  }
  .cta-text {
    font-size: 44px;
    font-weight: 700;
    color: #FFFFFF;
    letter-spacing: 0.01em;
  }
  .cta-arrow {
    font-size: 40px;
    color: #FFFFFF;
  }
</style>
</head>
<body>
<div class="ad-container">
  <div class="grid-lines"></div>
  <div class="circle-bg"></div>
  <div class="circle-bg-2"></div>
  <div class="circle-bg-3"></div>
  <div class="warm-glow"></div>
  <div class="gold-glow"></div>

  <div class="top-section">
    <div class="tag">
      <div class="tag-dot"></div>
      <span class="tag-text">T.O.P 사업단</span>
    </div>
    <div class="headline">혼자가 아닙니다</div>
  </div>

  <div class="middle-section">
    <div class="accent-badge">
      <div class="accent-number">30여종</div>
      <div class="accent-sub">무상 DB 지원</div>
    </div>
    <div class="sub-text">1:1 밀착 코칭 시스템</div>
  </div>

  <div class="bullets-section">
    <div class="bullet-item">
      <div class="bullet-icon"></div>
      <span class="bullet-text">DB + 코칭 + 매니저 3중 지원</span>
    </div>
    <div class="bullet-item">
      <div class="bullet-icon"></div>
      <span class="bullet-text">1:1 전담 매니저 밀착 케어</span>
    </div>
    <div class="bullet-item">
      <div class="bullet-icon"></div>
      <span class="bullet-text">30여종 무상 DB 즉시 지원</span>
    </div>
  </div>

  <div class="bottom-section">
    <div class="cta-button">
      <span class="cta-text">T.O.P 사업단 | 지금 시작하기</span>
      <span class="cta-arrow">→</span>
    </div>
  </div>
</div>
</body>
</html>"""

HTML_600x500 = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@300;400;500;700;900&display=swap" rel="stylesheet">
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  body {
    width: 600px;
    height: 500px;
    overflow: hidden;
    font-family: 'Noto Sans KR', sans-serif;
    background: #0A1628;
  }
  .ad-container {
    position: relative;
    width: 600px;
    height: 500px;
    background: linear-gradient(145deg, #0A1628 0%, #0F2040 50%, #0A1628 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 36px 40px 32px;
    overflow: hidden;
  }

  /* Shield circles */
  .circle-bg {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 420px;
    height: 420px;
    border: 1.5px solid rgba(76,175,80,0.12);
    border-radius: 50%;
    pointer-events: none;
  }
  .circle-bg-2 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 300px;
    height: 300px;
    border: 1px solid rgba(255,193,7,0.08);
    border-radius: 50%;
    pointer-events: none;
  }

  /* Warm glow */
  .warm-glow {
    position: absolute;
    bottom: -80px;
    left: 50%;
    transform: translateX(-50%);
    width: 500px;
    height: 300px;
    background: radial-gradient(ellipse, rgba(76,175,80,0.09) 0%, transparent 65%);
    pointer-events: none;
  }

  /* Grid */
  .grid-lines {
    position: absolute;
    inset: 0;
    background-image:
      linear-gradient(rgba(76,175,80,0.04) 1px, transparent 1px),
      linear-gradient(90deg, rgba(76,175,80,0.04) 1px, transparent 1px);
    background-size: 40px 40px;
  }

  /* Top tag */
  .top-section {
    position: relative;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .tag {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: rgba(255,193,7,0.10);
    border: 1px solid rgba(255,193,7,0.35);
    border-radius: 20px;
    padding: 7px 18px;
  }
  .tag-dot {
    width: 7px;
    height: 7px;
    background: #FFC107;
    border-radius: 50%;
    box-shadow: 0 0 6px rgba(255,193,7,0.7);
  }
  .tag-text {
    font-size: 22px;
    font-weight: 600;
    color: #FFC107;
    letter-spacing: 0.05em;
  }

  .headline-small {
    font-size: 34px;
    font-weight: 800;
    color: rgba(255,255,255,0.9);
    text-align: center;
    letter-spacing: -0.02em;
  }

  /* Center accent */
  .center-section {
    position: relative;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
  }

  .accent-big {
    font-size: 96px;
    font-weight: 900;
    color: #4CAF50;
    line-height: 1.0;
    letter-spacing: -0.03em;
    text-align: center;
    text-shadow: 0 0 40px rgba(76,175,80,0.5);
  }
  .accent-label {
    font-size: 40px;
    font-weight: 700;
    color: rgba(255,255,255,0.65);
    text-align: center;
    letter-spacing: 0.01em;
  }

  /* Support line */
  .support-line {
    position: relative;
    z-index: 10;
    font-size: 28px;
    font-weight: 500;
    color: rgba(255,255,255,0.6);
    text-align: center;
    letter-spacing: -0.01em;
  }
  .support-line strong {
    color: rgba(255,255,255,0.85);
    font-weight: 700;
  }

  /* CTA */
  .cta-button {
    position: relative;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: linear-gradient(135deg, #4CAF50, #45a049);
    border-radius: 10px;
    padding: 14px 36px;
    width: 100%;
    box-shadow: 0 6px 30px rgba(76,175,80,0.35);
  }
  .cta-text {
    font-size: 30px;
    font-weight: 700;
    color: #FFFFFF;
    letter-spacing: 0.01em;
  }
  .cta-arrow {
    font-size: 28px;
    color: rgba(255,255,255,0.9);
  }
</style>
</head>
<body>
<div class="ad-container">
  <div class="grid-lines"></div>
  <div class="circle-bg"></div>
  <div class="circle-bg-2"></div>
  <div class="warm-glow"></div>

  <div class="top-section">
    <div class="tag">
      <div class="tag-dot"></div>
      <span class="tag-text">T.O.P 사업단</span>
    </div>
    <div class="headline-small">혼자가 아닙니다</div>
  </div>

  <div class="center-section">
    <div class="accent-big">30여종 DB</div>
    <div class="accent-label">무상 지원</div>
  </div>

  <div class="support-line">
    <strong>DB + 코칭 + 매니저</strong> 3중 지원
  </div>

  <div class="cta-button">
    <span class="cta-text">T.O.P 사업단 | 지금 시작하기</span>
    <span class="cta-arrow">→</span>
  </div>
</div>
</body>
</html>"""


async def render_html_to_png(html_content: str, output_path: str, width: int, height: int):
    """Render HTML string to PNG using Playwright."""
    async with async_playwright() as p:
        browser = await p.chromium.launch(args=["--no-sandbox"])
        page = await browser.new_page(
            viewport={"width": width, "height": height},
            device_scale_factor=1.0
        )
        await page.set_content(html_content, wait_until="networkidle")
        # Extra wait for Google Fonts
        await page.wait_for_timeout(2500)
        await page.screenshot(path=output_path, full_page=False, clip={
            "x": 0, "y": 0, "width": width, "height": height
        })
        await browser.close()
    print(f"  Rendered: {output_path}")


def resize_to_300x250(input_path: str, output_path: str):
    """Resize a 600x500 image to 300x250 using PIL high-quality Lanczos."""
    img = Image.open(input_path)
    resized = img.resize((300, 250), Image.LANCZOS)
    resized.save(output_path, "PNG", optimize=True)
    print(f"  Resized to 300x250: {output_path}")


async def main():
    print("=== Angle E — Display Ad Renderer ===")
    print("Design: #49 Spikes 신뢰/보호 (Trust/Protection)\n")

    # 1. 1200×628
    out_1200x628 = str(OUTPUT_DIR / "display-1200x628.png")
    print("1. Rendering 1200×628 (landscape)...")
    await render_html_to_png(HTML_1200x628, out_1200x628, 1200, 628)

    # 2. 1200×1200
    out_1200x1200 = str(OUTPUT_DIR / "display-1200x1200.png")
    print("2. Rendering 1200×1200 (square)...")
    await render_html_to_png(HTML_1200x1200, out_1200x1200, 1200, 1200)

    # 3. 300×250: render at 600×500, then resize
    tmp_600x500 = str(OUTPUT_DIR / "_tmp_600x500.png")
    out_300x250 = str(OUTPUT_DIR / "display-300x250.png")
    print("3. Rendering 600×500 (for 300×250)...")
    await render_html_to_png(HTML_600x500, tmp_600x500, 600, 500)
    print("   Resizing to 300×250...")
    resize_to_300x250(tmp_600x500, out_300x250)

    # Clean up temp file
    os.remove(tmp_600x500)
    print(f"   Temp file removed.")

    print("\n=== All ads rendered successfully! ===")
    print(f"Output directory: {OUTPUT_DIR}")
    for f in sorted(OUTPUT_DIR.iterdir()):
        if f.suffix == ".png":
            size_kb = f.stat().st_size // 1024
            print(f"  {f.name}: {size_kb} KB")


if __name__ == "__main__":
    asyncio.run(main())
