#!/usr/bin/env python3
"""Generate vector typography logos via Recraft V4 Vector API."""
import os, json, requests, sys
from pathlib import Path

API_KEY = os.environ.get("RECRAFT_API_KEY")
BASE = "https://external.api.recraft.ai/v1"
HEADERS = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
OUT = Path("/root/.openclaw/workspace/phat-foods/logos")
OUT.mkdir(parents=True, exist_ok=True)

PROMPTS = {
    # OLEVIA variants
    "olevia_serif": (
        'Typographic wordmark logo for premium olive oil brand, the word "Olevia" '
        'in elegant modern serif typeface, clean letterforms with subtle olive branch '
        'integrated into the letter V, sophisticated minimalist design, dark green text '
        'on white background, luxury food branding, centered composition, vector logo'
    ),
    "olevia_script": (
        'Typographic wordmark logo, the word "Olevia" in flowing calligraphic script, '
        'connected letterforms with elegant swashes, olive green ink color, organic and '
        'natural feel, premium food brand identity, white background, balanced composition, '
        'vector logo design'
    ),
    "olevia_geometric": (
        'Modern geometric wordmark logo, the word "OLEVIA" in clean sans-serif uppercase '
        'letters, geometric construction with precise proportions, subtle leaf or olive '
        'motif in negative space, dark olive green on white, contemporary minimalist '
        'branding, centered, vector logo'
    ),
    "olevia_classic": (
        'Classic typographic logo for artisan olive oil brand, the word "Olevia" in '
        'refined transitional serif typeface, generous letter spacing, small olive '
        'illustration above the i dot, deep forest green, timeless Italian-inspired '
        'elegance, white background, vector logo'
    ),

    # ARIA variants
    "aria_serif": (
        'Typographic wordmark logo for premium olive oil brand, the word "Aria" '
        'in elegant modern serif typeface, clean refined letterforms, subtle wind or '
        'breeze motif integrated into the letter A, sophisticated minimalist design, '
        'dark green text on white background, luxury food branding, centered, vector logo'
    ),
    "aria_script": (
        'Typographic wordmark logo, the word "Aria" in graceful flowing script, '
        'airy and light calligraphic strokes, connected letters with elegant ascenders, '
        'olive green color, organic premium feel, white background, centered composition, '
        'vector logo design'
    ),
    "aria_geometric": (
        'Modern geometric wordmark logo, the word "ARIA" in clean sans-serif uppercase '
        'letters, architectural precision, balanced negative space, subtle leaf motif '
        'incorporated into the A letterform, dark olive green on white, minimal '
        'contemporary design, vector logo'
    ),
    "aria_classic": (
        'Classic typographic logo for artisan olive oil brand, the word "Aria" in '
        'refined Italian-style serif typeface, generous tracking, small olive branch '
        'ornament above, deep emerald green, timeless elegance, white background, '
        'vector logo'
    ),
}

def generate(name, prompt):
    print(f"Generating {name}...")
    body = {
        "prompt": prompt,
        "model": "recraftv4_vector",
        "n": 2,
        "size": "1:1",
        "response_format": "url",
    }
    r = requests.post(f"{BASE}/images/generations", headers=HEADERS, json=body, timeout=120)
    if r.status_code != 200:
        print(f"  ERROR {r.status_code}: {r.text[:300]}")
        return []
    data = r.json()
    urls = []
    for i, img in enumerate(data.get("data", [])):
        url = img.get("url", "")
        urls.append(url)
        # Download SVG
        svg_r = requests.get(url, timeout=60)
        fname = f"{name}_{i+1}.svg"
        (OUT / fname).write_bytes(svg_r.content)
        print(f"  Saved {fname} ({len(svg_r.content)} bytes)")
    return urls

if __name__ == "__main__":
    results = {}
    for name, prompt in PROMPTS.items():
        urls = generate(name, prompt)
        results[name] = urls
    
    # Save results index
    with open(OUT / "results.json", "w") as f:
        json.dump(results, f, indent=2)
    
    print(f"\nDone! {sum(len(v) for v in results.values())} logos saved to {OUT}")
