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

import sys
import os
import base64
import time
import re
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-1080x1080.png")
BG_TEMP_PATH = Path("/tmp/incar_support_bg_1080x1080.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. Square 1:1 composition. No money, no rockets, no complex tech equipment, no icons or illustrations. Photo-realistic quality."""

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: 1080px; height: 1080px; overflow: hidden; font-family: 'Pretendard', 'Noto Sans KR', sans-serif; }
  .container {
    position: relative;
    width: 1080px;
    height: 1080px;
    background-color: #EBF2FF;
  }
  .bg-image {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background-image: var(--bg-url);
    background-size: cover;
    background-position: center;
  }
  /* 화이트 반투명 오버레이 (텍스트 가독성) — opacity 0.4 */
  .white-overlay {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(248, 249, 252, 0.72);
  }
  /* 전체 레이아웃 컨테이너 */
  .layout {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 52px 72px 60px;
  }
  /* 상단 10%: 브랜드 라인 */
  .brand-line {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 30px;
  }
  .brand-line-bar {
    flex: 1;
    height: 2px;
    background: linear-gradient(to right, transparent, rgba(43, 87, 151, 0.3));
  }
  .brand-line-bar.right {
    background: linear-gradient(to left, transparent, rgba(43, 87, 151, 0.3));
  }
  .brand-name {
    font-size: 36px;
    font-weight: 600;
    color: #2B5797;
    letter-spacing: 0.05em;
    text-align: center;
    white-space: nowrap;
  }
  /* 헤드라인 영역 (상단~중앙 35%) */
  .headline-area {
    width: 100%;
    text-align: center;
    margin-bottom: 32px;
  }
  .tagline {
    font-size: 28px;
    font-weight: 500;
    color: #5B9BD5;
    letter-spacing: 0.04em;
    margin-bottom: 14px;
  }
  .headline {
    font-size: 56px;
    font-weight: 800;
    color: #1B365D;
    line-height: 1.25;
    letter-spacing: -0.03em;
    word-break: keep-all;
  }
  /* 구분선 */
  .section-divider {
    display: flex;
    align-items: center;
    gap: 14px;
    width: 100%;
    margin-bottom: 30px;
  }
  .divider-line {
    flex: 1;
    height: 1.5px;
    background: rgba(43, 87, 151, 0.18);
  }
  .divider-diamond {
    width: 8px; height: 8px;
    background: #C5A572;
    transform: rotate(45deg);
  }
  /* 지원 항목 리스트 (중앙 30%) */
  .support-list {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 18px;
    margin-bottom: 38px;
  }
  .support-item {
    display: flex;
    align-items: center;
    gap: 18px;
    background: rgba(255, 255, 255, 0.75);
    border: 1.5px solid rgba(43, 87, 151, 0.12);
    border-radius: 10px;
    padding: 18px 28px;
  }
  .support-icon {
    width: 10px; height: 10px;
    border-radius: 50%;
    background: #2B5797;
    flex-shrink: 0;
  }
  .support-text {
    font-size: 40px;
    font-weight: 600;
    color: #1B365D;
    letter-spacing: -0.02em;
    line-height: 1.2;
  }
  .support-text .accent {
    color: #C5A572;
    font-weight: 700;
  }
  /* CTA 버튼 영역 (하단 25%) */
  .cta-area {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    margin-top: auto;
  }
  .cta-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #2B5797;
    color: #FFFFFF;
    font-size: 40px;
    font-weight: 700;
    padding: 20px 80px;
    border-radius: 8px;
    letter-spacing: -0.01em;
    min-width: 400px;
  }
  .cta-sub {
    font-size: 28px;
    font-weight: 500;
    color: #5B9BD5;
    letter-spacing: -0.01em;
    text-align: center;
  }
</style>
</head>
<body>
<div class="container">
  <div class="bg-image"></div>
  <div class="white-overlay"></div>
  <div class="layout">
    <!-- 상단 브랜드 라인 -->
    <div class="brand-line">
      <div class="brand-line-bar"></div>
      <span class="brand-name">인카금융서비스</span>
      <div class="brand-line-bar right"></div>
    </div>
    <!-- 헤드라인 -->
    <div class="headline-area">
      <div class="tagline">자본 없이 시작, 상장사가 뒤를 받쳐줍니다</div>
      <div class="headline">무자본창업,<br>인카가 도와드립니다</div>
    </div>
    <!-- 구분선 -->
    <div class="section-divider">
      <div class="divider-line"></div>
      <div class="divider-diamond"></div>
      <div class="divider-line"></div>
    </div>
    <!-- 지원 항목 3줄 -->
    <div class="support-list">
      <div class="support-item">
        <div class="support-icon"></div>
        <span class="support-text">DB 자동화 시스템 제공</span>
      </div>
      <div class="support-item">
        <div class="support-icon"></div>
        <span class="support-text">AI 무상 세팅 지원</span>
      </div>
      <div class="support-item">
        <div class="support-icon"></div>
        <span class="support-text">정착금 <span class="accent">최대 1,000만원</span></span>
      </div>
    </div>
    <!-- CTA -->
    <div class="cta-area">
      <div class="cta-button">무료 상담 신청</div>
      <div class="cta-sub">지금 바로 시작하세요 — 상담은 무료입니다</div>
    </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": 1080, "height": 1080})
            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 영업지원 배너 1080x1080 생성 ===")
    start = time.time()

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

    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()
