#!/usr/bin/env python3
"""인카다이렉트 M1-3 영업지원 배너 1200x628 생성 스크립트
hybrid-image 파이프라인: Gemini 배경 + HTML/CSS 오버레이 (Playwright)
"""

import sys
import os
import base64
import time
import json
import re
import subprocess
import tempfile
from pathlib import Path

sys.path.insert(0, "/home/jay/workspace/tools/ai-image-gen")

OUTPUT_PATH = Path("/home/jay/workspace/output/google-ads/banners/incar-support-1200x628.png")
BG_TEMP_PATH = Path("/tmp/incar_support_bg_1200x628.jpg")

GEMINI_BG_PROMPT = """Bright, clean coworking space interior. One person's side profile working on a laptop at a personal desk with two monitors, face not visible. A single coffee cup on the desk. Desktop screen slightly blurred. Warm natural sunlight streaming through large windows. Comfortable, well-organized morning work atmosphere. Soft warm tones — cream, warm white, light wood. Inviting, approachable, professional but not cold. No money, no rockets, no complex tech equipment, no icons or illustrations. Photo-realistic quality, wide horizontal crop."""

HTML_TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
  @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@400;500;700;900&display=swap');
  * { margin: 0; padding: 0; box-sizing: border-box; }
  body { width: 1200px; height: 628px; overflow: hidden; font-family: 'Pretendard', 'Noto Sans KR', sans-serif; }
  .container {
    position: relative;
    width: 1200px;
    height: 628px;
    background-color: #f5f6f8;
  }
  .bg-image {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background-image: var(--bg-url);
    background-size: cover;
    background-position: center right;
  }
  /* 좌측 화이트→투명 그라데이션 오버레이 (텍스트 가독성) */
  .gradient-overlay {
    position: absolute;
    top: 0; left: 0;
    width: 75%;
    height: 100%;
    background: linear-gradient(
      to right,
      rgba(248, 249, 252, 1.0) 0%,
      rgba(248, 249, 252, 0.97) 40%,
      rgba(248, 249, 252, 0.85) 60%,
      rgba(248, 249, 252, 0.5) 80%,
      rgba(248, 249, 252, 0.0) 100%
    );
  }
  /* 텍스트 영역 (좌측 60%) */
  .text-area {
    position: absolute;
    top: 0; left: 0;
    width: 680px;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 50px 60px;
    gap: 22px;
  }
  /* 브랜드 뱃지 */
  .brand-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: rgba(43, 87, 151, 0.10);
    border: 1.5px solid rgba(43, 87, 151, 0.30);
    border-radius: 6px;
    padding: 7px 16px;
    width: fit-content;
  }
  .brand-badge-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: #2B5797;
  }
  .brand-badge-text {
    font-size: 32px;
    font-weight: 600;
    color: #2B5797;
    letter-spacing: -0.02em;
    line-height: 1.2;
  }
  /* 헤드라인 */
  .headline {
    font-size: 50px;
    font-weight: 800;
    color: #1B365D;
    line-height: 1.25;
    letter-spacing: -0.03em;
    word-break: keep-all;
  }
  /* 서브카피 구분선 */
  .divider {
    width: 48px;
    height: 3px;
    background: #C5A572;
    border-radius: 2px;
    margin: 2px 0;
  }
  /* 서브카피 — 지원 항목 */
  .subcopy {
    font-size: 32px;
    font-weight: 500;
    color: #2B5797;
    letter-spacing: -0.02em;
    line-height: 1.4;
  }
  .subcopy .pipe {
    color: #C5A572;
    font-weight: 600;
    margin: 0 6px;
  }
  .subcopy .accent {
    color: #C5A572;
    font-weight: 700;
  }
  /* CTA 버튼 */
  .cta-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #2B5797;
    color: #FFFFFF;
    font-size: 34px;
    font-weight: 700;
    padding: 16px 42px;
    border-radius: 8px;
    width: fit-content;
    letter-spacing: -0.01em;
    margin-top: 6px;
  }
</style>
</head>
<body>
<div class="container">
  <div class="bg-image"></div>
  <div class="gradient-overlay"></div>
  <div class="text-area">
    <div class="brand-badge">
      <div class="brand-badge-dot"></div>
      <span class="brand-badge-text">인카금융서비스</span>
    </div>
    <div class="headline">무자본창업,<br>인카가 도와드립니다</div>
    <div class="divider"></div>
    <div class="subcopy">
      DB 자동화<span class="pipe">|</span>AI 무상 세팅<span class="pipe">|</span>정착금 <span class="accent">최대 1,000만원</span>
    </div>
    <div class="cta-button">무료 상담 신청</div>
  </div>
</div>
</body>
</html>"""


def get_token():
    import gcloud_auth
    return gcloud_auth.get_access_token()


def generate_bg_image(token: str) -> bool:
    import requests
    model_ids = ["gemini-3.1-flash-image-preview", "gemini-3-flash-preview", "gemini-3-pro-image-preview"]
    gemini_api_base = "https://generativelanguage.googleapis.com/v1beta"

    for model_id in model_ids:
        print(f"  [BG] Gemini 배경 생성 시도: {model_id}")
        url = f"{gemini_api_base}/models/{model_id}:generateContent"
        headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
        payload = {
            "contents": [{"parts": [{"text": GEMINI_BG_PROMPT}]}],
            "generationConfig": {"responseModalities": ["IMAGE", "TEXT"]},
        }
        try:
            resp = requests.post(url, headers=headers, json=payload, timeout=120)
            if resp.status_code in (400, 403, 404):
                print(f"  [BG] {model_id} 실패 ({resp.status_code}), 다음 시도...")
                continue
            resp.raise_for_status()
            data = resp.json()
            parts = data.get("candidates", [{}])[0].get("content", {}).get("parts", [])
            for part in parts:
                if "inlineData" in part:
                    img_bytes = base64.b64decode(part["inlineData"]["data"])
                    BG_TEMP_PATH.write_bytes(img_bytes)
                    print(f"  [BG] 배경 이미지 저장: {BG_TEMP_PATH} ({len(img_bytes):,} bytes)")
                    return True
        except Exception as e:
            print(f"  [BG] {model_id} 오류: {e}")
            continue

    # API key fallback
    api_key_file = "/home/jay/workspace/.env.keys"
    api_key = None
    if os.path.exists(api_key_file):
        content = open(api_key_file).read()
        m = re.search(r'GEMINI_API_KEY\s*=\s*["\']?([A-Za-z0-9_\-]+)["\']?', content)
        if m:
            api_key = m.group(1)
    if not api_key:
        env_path = "/home/jay/workspace/.env"
        if os.path.exists(env_path):
            content = open(env_path).read()
            m = re.search(r'GEMINI_API_KEY\s*=\s*["\']?([A-Za-z0-9_\-]+)["\']?', content)
            if m:
                api_key = m.group(1)

    if api_key:
        import requests
        for model_id in ["gemini-3.1-flash-image-preview", "gemini-3-flash-preview"]:
            print(f"  [BG] API key 방식 시도: {model_id}")
            url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_id}:generateContent?key={api_key}"
            headers = {"Content-Type": "application/json"}
            payload = {
                "contents": [{"parts": [{"text": GEMINI_BG_PROMPT}]}],
                "generationConfig": {"responseModalities": ["IMAGE", "TEXT"]},
            }
            try:
                resp = requests.post(url, headers=headers, json=payload, timeout=120)
                if resp.status_code in (400, 403, 404):
                    err_data = {}
                    try:
                        err_data = resp.json()
                    except Exception:
                        pass
                    print(f"  [BG] {model_id} 실패 ({resp.status_code}): {str(err_data)[:200]}")
                    continue
                resp.raise_for_status()
                data = resp.json()
                parts = data.get("candidates", [{}])[0].get("content", {}).get("parts", [])
                for part in parts:
                    if "inlineData" in part:
                        img_bytes = base64.b64decode(part["inlineData"]["data"])
                        BG_TEMP_PATH.write_bytes(img_bytes)
                        print(f"  [BG] 배경 이미지 저장: {BG_TEMP_PATH} ({len(img_bytes):,} bytes)")
                        return True
            except Exception as e:
                print(f"  [BG] {model_id} 오류: {e}")
                continue

    print("  [BG] Gemini 배경 생성 실패 — 그라데이션 폴백 사용")
    return False


def render_with_playwright(bg_available: bool) -> bool:
    from playwright.sync_api import sync_playwright

    html = HTML_TEMPLATE
    if bg_available and BG_TEMP_PATH.exists():
        bg_uri = f"file://{BG_TEMP_PATH}"
        html = html.replace("background-image: var(--bg-url);",
                            f"background-image: url('{bg_uri}');")
    else:
        # 폴백: 밝은 그라데이션 배경
        html = html.replace("background-image: var(--bg-url);",
                            "background: linear-gradient(135deg, #EBF2FF 0%, #D6E8FF 50%, #C5D9F5 100%);")

    with tempfile.NamedTemporaryFile(suffix=".html", mode="w", encoding="utf-8", delete=False) as f:
        f.write(html)
        tmp_html = f.name

    try:
        with sync_playwright() as p:
            browser = p.chromium.launch()
            page = browser.new_page(viewport={"width": 1200, "height": 628})
            page.goto(f"file://{tmp_html}", wait_until="networkidle")
            page.wait_for_timeout(2000)
            OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
            page.screenshot(path=str(OUTPUT_PATH), type="png")
            browser.close()
        print(f"  [캡처] {OUTPUT_PATH}")
        return True
    finally:
        os.unlink(tmp_html)


def main():
    print("=== 인카다이렉트 M1-3 영업지원 배너 1200x628 생성 ===")
    start = time.time()

    # 1. Gemini 배경 생성
    try:
        token = get_token()
        bg_ok = generate_bg_image(token)
    except Exception as e:
        print(f"  [인증] 토큰 획득 실패: {e} — 폴백 배경 사용")
        bg_ok = False

    # 2. HTML 오버레이 + Playwright 캡처
    ok = render_with_playwright(bg_ok)

    elapsed = time.time() - start
    if ok and OUTPUT_PATH.exists():
        size_kb = OUTPUT_PATH.stat().st_size / 1024
        print(f"=== 완료: {OUTPUT_PATH} ({size_kb:.0f} KB, {elapsed:.1f}초) ===")
        sys.exit(0)
    else:
        print("=== 실패 ===")
        sys.exit(1)


if __name__ == "__main__":
    main()
