#!/usr/bin/env bash
# Level-Splash-Bilder fuer Logistik. Gleicher Stil wie die Stadt-Bilder
# (Flat Scandinavian Illustration), aber breitere Szene pro Level.
#
# Aufruf:
#   set -a; source App/.env.local; set +a
#   App/sims/logistik/scripts/generate-splash-images.sh
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=(
  [level-1]="$PROMPT_BASE Scene: Wide panoramic view over central Europe (the Alpine DACH region). A small red delivery truck driving on a winding Alpine highway between forested mountains, Austrian valleys with small villages and church towers in the distance, snow-capped Alps dominating the background. Blue sky with a warm sunrise glow. The feel is a friendly 'starting out' first-day atmosphere, wide horizons, adventurous but manageable."
  [level-2]="$PROMPT_BASE Scene: Panoramic central European logistics hub view with multiple trucks on parallel autobahn lanes, a freight train crossing a viaduct in the foreground, container terminals with stacks of colourful containers in the middle distance, mixed industrial-rural landscape, wider geographic spread with glimpses of Hamburg harbour on the left, Alps on the right. Feeling of scaling up and coordinating multiple vehicles."
  [level-3]="$PROMPT_BASE Scene: Grand European panorama showing the full continent scale with major port cities Rotterdam and Hamburg on the North Sea, container ships, freight trains crossing continental Europe on multiple rail corridors, trucks on transnational highways, the Alps with Brenner pass, and southern ports Marseille and Trieste visible. Overcast with dramatic sky, feeling of complex multi-modal logistics empire."
)

TARGETS=("$@")
if [ ${#TARGETS[@]} -eq 0 ]; then TARGETS=(level-1 level-2 level-3); fi

for id in "${TARGETS[@]}"; do
  prompt="${PROMPTS[$id]}"
  if [ -z "$prompt" ]; then echo "x $id: kein Prompt definiert"; continue; fi
  if [ -f "${id}.png" ] && [ -z "$FORCE" ]; then
    echo "ok ${id}.png 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 "${id}.png"
  echo "ok ${id}.png ($(du -h "${id}.png" | cut -f1))"
  sleep 2
done
