#!/usr/bin/env bash
# Splash- und Cockpit-Bilder fuer Entscheidungstag im offiziellen
# GeoGraSim-Bildstil (Flat Scandinavian Illustration).
#
# Aufruf:
#   set -a; source App/.env.local; set +a
#   App/sims/entscheidungstag/scripts/generate-splash-images.sh
#   App/sims/entscheidungstag/scripts/generate-splash-images.sh splash-winter
set -e
: "${OPENAI_API_KEY:?Env-Variable OPENAI_API_KEY muss gesetzt sein}"

mkdir -p "$(dirname "$0")/../assets/splash"
cd "$(dirname "$0")/../assets/splash"

PROMPT_BASE="Flat Scandinavian illustration, wide panoramic landscape filling the entire 16:9 frame edge-to-edge, painted style with clean vector shapes, dark forest green (#1f4b37) and sage green (#4a7c4e) tones, yellow-mustard (#e8c547) accents, beige (#e8e4d8) buildings and valleys, white highlights. No text, no watermark, no logos, no borders, no frame."

declare -A PROMPTS=(
  [splash-winter]="$PROMPT_BASE Scene: Wide panoramic Austrian winter landscape at dawn. A small beige family house in the foreground center with a warm yellow-mustard glow in its window and gentle smoke from the chimney rising into the cold sky. Dark forest green pine trees on either side dusted with white snow. Sage green rolling hills in the middle distance. A small footpath winds from the house toward a distant village with church tower silhouettes. Soft yellow-mustard sunrise glow on the eastern horizon, the rest of the sky a calm muted beige-blue. Snow lies softly on the rooftops and ground. The mood is peaceful, contemplative, the start of a winter day full of small everyday decisions."
  [splash-house]="$PROMPT_BASE Scene: Cross-section panoramic view of a single Austrian family house in winter, showing dark forest green roof tiles, beige walls, yellow-mustard warm windows. Outside the house: snow-covered ground, a small evergreen forest of dark forest green pines on the right, sage green hills behind. The house chimney has gentle white smoke. Wide horizontal composition emphasising the house and its environment, no people visible, calm composition suitable as a sim splash."
  [card-image]="$PROMPT_BASE Scene: Compact horizontal composition for a small card preview image, 16:9 ratio. A cozy beige Austrian family house in winter snow with a yellow-mustard glow in the window, surrounded by dark forest green pine trees and sage green hills in the background. Calm, friendly, inviting feel. Single house centered, clean simple composition that reads well at small sizes."
)

TARGETS=("$@")
if [ ${#TARGETS[@]} -eq 0 ]; then TARGETS=(splash-winter card-image); fi

for id in "${TARGETS[@]}"; do
  prompt="${PROMPTS[$id]}"
  if [ -z "$prompt" ]; then echo "x $id: kein Prompt definiert"; continue; fi
  out="${id}.png"
  if [ -f "$out" ] && [ -z "$FORCE" ]; then
    echo "ok $out schon vorhanden (FORCE=1 zum Ueberschreiben)"
    continue
  fi
  echo "-> ${id}: generiere ..."
  payload=$(PROMPT="$prompt" python -c 'import json,os; print(json.dumps({"model":"dall-e-3","prompt":os.environ["PROMPT"],"n":1,"size":"1792x1024","quality":"standard"}))')
  resp=$(curl -s https://api.openai.com/v1/images/generations \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -H "Content-Type: application/json" \
    -d "$payload")
  url=$(echo "$resp" | python -c 'import sys,json
d=json.load(sys.stdin)
print(d.get("data",[{}])[0].get("url",""))' 2>/dev/null)
  if [ -z "$url" ]; then
    err=$(echo "$resp" | python -c 'import sys,json
try: print(json.load(sys.stdin).get("error",{}).get("message","?"))
except: print("unparseable")' 2>/dev/null)
    echo "x ${id}: Fehler - $err"
    continue
  fi
  curl -s "$url" -o "$out"
  echo "ok $out ($(du -h "$out" | cut -f1))"
  sleep 2
done
