#!/usr/bin/env python3
"""Cell-7 서울대보험쌤 × 정당한 보상 배너 생성 (v3.2 최종본)
- meta-feed-1080x1080.png  (Template B: 전체 배경 + 크림 패널)
- google-resp-1200x628.png (Template A: 좌측 텍스트 + 우측 배경)
"""

from __future__ import annotations

import base64
import sys
import time
from pathlib import Path

import requests
from playwright.sync_api import sync_playwright

# ── 경로 설정 ─────────────────────────────────────────────────────────────────
BASE_DIR = Path("/home/jay/workspace/tools/ai-image-gen")
OUTPUT_DIR = Path("/home/jay/workspace/output/banners/cell-7-snu-fair")
FALLBACK_BG = Path("/home/jay/workspace/output/meta-ads/concept-catalog/45-oneshow-finance-luxury/bg.jpg")

BG_1080_PATH = OUTPUT_DIR / "bg-cell7-1080.png"
BG_1200_PATH = OUTPUT_DIR / "bg-cell7-1200.png"

GEMINI_API_BASE = "https://generativelanguage.googleapis.com/v1beta"
GEMINI_MODEL = "gemini-2.0-flash-preview-image-generation"
GEMINI_FALLBACK_MODEL = "gemini-2.0-flash-exp"


# ── 배경 프롬프트 ──────────────────────────────────────────────────────────────

PROMPT_1080 = (
    "Warm cozy study room with mahogany bookshelves filled with books, "
    "a vintage desk lamp casting warm golden light, leather armchair, "
    "reading glasses on an open notebook, warm brown and amber tones, "
    "intimate and professional atmosphere, photorealistic interior photography, "
    "no people, no text, no logos. Square 1:1 format."
)

PROMPT_1200 = (
    "Wide panoramic view of a warm professional study with floor-to-ceiling "
    "mahogany bookshelves, vintage brass desk lamp glowing warmly, leather-bound "
    "books and a coffee cup on the desk, golden hour sunlight filtering through "
    "curtains, warm brown and amber atmosphere, photorealistic, no people, no text, "
    "no logos. Wide landscape 1.91:1 format."
)


# ── Gemini 배경 생성 ───────────────────────────────────────────────────────────

def get_api_key() -> str:
    sys.path.insert(0, str(BASE_DIR))
    import gcloud_auth
    return gcloud_auth.get_api_key("GEMINI_API_KEY") or "AIzaSyDEy7IcOoTbQsI4nxfPOw3ZFbYqEL_PgFU"


def generate_gemini_background(prompt: str, out_path: Path, label: str) -> bool:
    """Gemini API로 배경 이미지 생성. 성공 시 True 반환."""
    if out_path.exists() and out_path.stat().st_size > 30_000:
        print(f"[{label}] 기존 배경 재사용: {out_path}")
        return True

    api_key = get_api_key()
    payload = {
        "contents": [{"parts": [{"text": prompt}]}],
        "generationConfig": {"responseModalities": ["IMAGE", "TEXT"]},
    }

    models = [GEMINI_MODEL, GEMINI_FALLBACK_MODEL, "gemini-1.5-flash"]
    for model in models:
        url = f"{GEMINI_API_BASE}/models/{model}:generateContent?key={api_key}"
        print(f"[{label}] Gemini 요청 중 (모델: {model})...")
        try:
            resp = requests.post(url, json=payload, timeout=120)
        except Exception as e:
            print(f"  요청 예외: {e}")
            continue

        if resp.status_code != 200:
            print(f"  HTTP {resp.status_code}: {resp.text[:200]}")
            continue

        data = resp.json()
        candidates = data.get("candidates", [])
        if not candidates:
            print(f"  candidates 없음")
            continue

        parts = candidates[0].get("content", {}).get("parts", [])
        image_part = next((p for p in parts if "inlineData" in p), None)
        if not image_part:
            print(f"  inlineData 없음. parts keys: {[list(p.keys()) for p in parts]}")
            continue

        img_bytes = base64.b64decode(image_part["inlineData"]["data"])
        out_path.write_bytes(img_bytes)
        print(f"  [OK] {out_path} ({len(img_bytes):,} bytes)")
        return True

    return False


# ── HTML 파일 저장 ─────────────────────────────────────────────────────────────

HTML_1080 = """\
<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=1080">
  <style>
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Black.otf') format('opentype'); font-weight: 900; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-ExtraBold.otf') format('opentype'); font-weight: 800; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Bold.otf') format('opentype'); font-weight: 700; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-SemiBold.otf') format('opentype'); font-weight: 600; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Medium.otf') format('opentype'); font-weight: 500; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Regular.otf') format('opentype'); font-weight: 400; }

    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    html, body { width: 1080px; height: 1080px; overflow: hidden; font-family: 'Pretendard', sans-serif; word-break: keep-all; }

    .canvas { position: relative; width: 1080px; height: 1080px; overflow: hidden; }
    .bg-image { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; object-position: center; }
    .dark-overlay { position: absolute; inset: 0; background: rgba(62, 39, 35, 0.58); }

    .text-panel {
      position: absolute;
      top: 50%; left: 50%;
      transform: translate(-50%, -50%);
      width: 860px;
      padding: 56px 48px;
      background: rgba(255, 248, 231, 0.82);
      border-radius: 12px;
      backdrop-filter: blur(4px);
      box-shadow: 0 8px 32px rgba(0,0,0,0.2);
      display: flex; flex-direction: column;
      align-items: center;
      gap: 28px; text-align: center;
    }

    .badge {
      display: inline-block;
      background: #C9A84C; color: #1A0E00;
      font-size: 40px; font-weight: 700;
      height: 52px; line-height: 52px;
      padding: 0 24px;
      border-radius: 6px;
      white-space: nowrap;
      letter-spacing: -0.03em;
    }

    .headline {
      font-size: 60px; font-weight: 700;
      color: #3E2723; line-height: 1.25;
      letter-spacing: -1.5px;
    }
    .headline .gold { color: #C9A84C; }

    .sub-copy {
      font-size: 44px; font-weight: 500;
      color: #A07828;
      white-space: nowrap;
      letter-spacing: -0.02em;
    }

    .sub-copy-2 {
      font-size: 44px; font-weight: 500;
      color: #A07828;
      white-space: nowrap;
      letter-spacing: -0.02em;
    }

    .cta-btn {
      display: inline-block;
      background: linear-gradient(135deg, #C9A84C 0%, #D4B87A 100%);
      color: #1A0E00;
      font-size: 48px; font-weight: 700;
      height: 76px; line-height: 76px;
      padding: 0 40px;
      min-width: 360px;
      width: auto;
      border-radius: 6px;
      box-shadow: 0 6px 24px rgba(201,168,76,0.45);
      white-space: nowrap;
      letter-spacing: -0.03em;
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="canvas">
    <img class="bg-image" src="bg-cell7-1080.png" alt="background">
    <div class="dark-overlay"></div>
    <div class="text-panel">
      <span class="badge">서울대보험쌤</span>
      <p class="headline">3년째 참았던 수수료,<br>저는 <span class="gold">바꿨어요</span></p>
      <p class="sub-copy">경력직 직전연봉 최대 50% 정착지원금</p>
      <p class="sub-copy-2">검증된 조직</p>
      <div class="cta-btn">수익 구조 직접 확인 →</div>
    </div>
  </div>
</body>
</html>
"""

HTML_1200 = """\
<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=1200">
  <style>
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Black.otf') format('opentype'); font-weight: 900; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-ExtraBold.otf') format('opentype'); font-weight: 800; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Bold.otf') format('opentype'); font-weight: 700; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-SemiBold.otf') format('opentype'); font-weight: 600; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Medium.otf') format('opentype'); font-weight: 500; }
    @font-face { font-family: 'Pretendard'; src: url('file:///home/jay/.local/share/fonts/Pretendard/Pretendard-Regular.otf') format('opentype'); font-weight: 400; }

    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    html, body { width: 1200px; height: 628px; overflow: hidden; font-family: 'Pretendard', sans-serif; word-break: keep-all; }

    .canvas { position: relative; width: 1200px; height: 628px; overflow: hidden; }
    .bg-image { position: absolute; right: 0; top: 0; width: 65%; height: 100%; object-fit: cover; object-position: center right; }

    .gradient-overlay {
      position: absolute; inset: 0;
      background: linear-gradient(
        to right,
        rgba(62,39,35,0.95) 0%,
        rgba(62,39,35,0.95) 40%,
        rgba(62,39,35,0.85) 52%,
        rgba(62,39,35,0.50) 68%,
        rgba(62,39,35,0.15) 82%,
        transparent 100%
      );
    }

    .accent-bar {
      position: absolute;
      top: 80px; left: 0;
      width: 4px; height: 460px;
      background: linear-gradient(to bottom, #C9A84C, #D4B87A);
      border-radius: 2px;
    }

    .text-area {
      position: absolute;
      top: 0; left: 0;
      width: 62%; height: 100%;
      padding: 48px 56px 48px 60px;
      display: flex; flex-direction: column;
      justify-content: center;
      gap: 20px;
    }

    .badge {
      display: inline-block;
      background: #C9A84C; color: #1A0E00;
      font-size: 40px; font-weight: 700;
      height: 48px; line-height: 48px;
      padding: 0 20px;
      border-radius: 6px;
      white-space: nowrap;
      letter-spacing: -0.03em;
      align-self: flex-start;
      margin-bottom: 6px;
    }

    .headline {
      font-size: 54px; font-weight: 700;
      color: #FFF8E7; line-height: 1.2;
      letter-spacing: -1.2px;
    }
    .headline .gold { color: #C9A84C; }

    .sub-copy {
      font-size: 44px; font-weight: 500;
      color: #C9A84C;
      text-shadow: 0 1px 3px rgba(0,0,0,0.5);
      white-space: nowrap;
      letter-spacing: -0.02em;
    }

    .sub-copy-2 {
      font-size: 40px; font-weight: 500;
      color: rgba(255,248,231,0.8);
      text-shadow: 0 1px 3px rgba(0,0,0,0.5);
      white-space: nowrap;
    }

    .cta-btn {
      display: inline-block;
      background: linear-gradient(135deg, #C9A84C 0%, #D4B87A 100%);
      color: #1A0E00;
      font-size: 44px; font-weight: 700;
      height: 56px; line-height: 56px;
      padding: 0 32px;
      min-width: 220px;
      width: auto;
      border-radius: 6px;
      box-shadow: 0 4px 16px rgba(201,168,76,0.35);
      white-space: nowrap;
      letter-spacing: -0.03em;
      align-self: flex-start;
      text-align: center;
      margin-top: 16px;
    }
  </style>
</head>
<body>
  <div class="canvas">
    <img class="bg-image" src="bg-cell7-1200.png" alt="background">
    <div class="gradient-overlay"></div>
    <div class="accent-bar"></div>
    <div class="text-area">
      <span class="badge">서울대보험쌤</span>
      <p class="headline">3년째 참았던 수수료,<br>저는 <span class="gold">바꿨어요</span></p>
      <p class="sub-copy">경력직 직전연봉 최대 50%<br>정착지원금 | 검증된 조직</p>
      <div class="cta-btn">수익 구조 직접 확인 →</div>
    </div>
  </div>
</body>
</html>
"""


# ── Playwright 렌더링 ──────────────────────────────────────────────────────────

def render_html_to_png(html_path: Path, width: int, height: int, out_path: Path) -> None:
    """Playwright로 HTML을 PNG로 캡처합니다."""
    with sync_playwright() as p:
        browser = p.chromium.launch()
        try:
            page = browser.new_page(viewport={"width": width, "height": height})
            page.goto(f"file://{html_path.resolve()}", wait_until="networkidle")
            page.wait_for_timeout(2500)  # 폰트 + 이미지 로딩 대기
            page.screenshot(
                path=str(out_path),
                type="png",
                clip={"x": 0, "y": 0, "width": width, "height": height},
            )
            size_kb = out_path.stat().st_size // 1024
            print(f"  [렌더링 완료] {out_path.name} ({size_kb} KB)")
        finally:
            browser.close()


# ── 메인 ──────────────────────────────────────────────────────────────────────

def main():
    print("=" * 60)
    print("Cell-7 서울대보험쌤 × 정당한 보상 배너 생성")
    print("=" * 60)

    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

    # ── Step 1: 배경 이미지 생성 ──────────────────────────────────────────────

    print("\n[Step 1] 배경 이미지 생성")

    ok_1080 = generate_gemini_background(PROMPT_1080, BG_1080_PATH, "1080x1080 배경")
    if not ok_1080:
        print(f"  Gemini 실패 → 폴백 사용: {FALLBACK_BG}")
        import shutil
        shutil.copy2(FALLBACK_BG, BG_1080_PATH)
        print(f"  폴백 복사 완료: {BG_1080_PATH}")

    ok_1200 = generate_gemini_background(PROMPT_1200, BG_1200_PATH, "1200x628 배경")
    if not ok_1200:
        print(f"  Gemini 실패 → 폴백 사용: {FALLBACK_BG}")
        import shutil
        shutil.copy2(FALLBACK_BG, BG_1200_PATH)
        print(f"  폴백 복사 완료: {BG_1200_PATH}")

    # ── Step 2: HTML 파일 저장 ────────────────────────────────────────────────

    print("\n[Step 2] HTML 파일 저장")

    html_1080_path = OUTPUT_DIR / "meta-feed-1080x1080.html"
    html_1200_path = OUTPUT_DIR / "google-resp-1200x628.html"

    html_1080_path.write_text(HTML_1080, encoding="utf-8")
    print(f"  저장: {html_1080_path}")

    html_1200_path.write_text(HTML_1200, encoding="utf-8")
    print(f"  저장: {html_1200_path}")

    # ── Step 3: Playwright 렌더링 ─────────────────────────────────────────────

    print("\n[Step 3] Playwright 렌더링")

    out_1080 = OUTPUT_DIR / "meta-feed-1080x1080.png"
    out_1200 = OUTPUT_DIR / "google-resp-1200x628.png"

    print("  1080x1080 렌더링 중...")
    render_html_to_png(html_1080_path, 1080, 1080, out_1080)

    print("  1200x628 렌더링 중...")
    render_html_to_png(html_1200_path, 1200, 628, out_1200)

    # ── 완료 보고 ─────────────────────────────────────────────────────────────

    print("\n" + "=" * 60)
    print("완료!")
    files = [BG_1080_PATH, BG_1200_PATH, html_1080_path, html_1200_path, out_1080, out_1200]
    for f in files:
        if f.exists():
            print(f"  OK  {f}  ({f.stat().st_size // 1024} KB)")
        else:
            print(f"  !!  {f}  (없음)")
    print("=" * 60)


if __name__ == "__main__":
    main()
