#!/usr/bin/env bash
set -euo pipefail

: "${EVOLINK_API_KEY:?Set EVOLINK_API_KEY first}"
api_base="${EVOLINK_API_BASE:-https://api.evolink.ai}"

payload='{
  "model": "gpt-image-2",
  "prompt": "A plain red square centered on a white background, simple geometric test image, no text",
  "size": "1:1",
  "resolution": "1K",
  "quality": "low",
  "n": 1
}'

created="$(curl -sS "$api_base/v1/images/generations" \
  -H "Authorization: Bearer $EVOLINK_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$payload")"

echo "$created"
task_id="$(printf '%s' "$created" | node -e 'let s="";process.stdin.on("data",d=>s+=d);process.stdin.on("end",()=>{const j=JSON.parse(s);console.log(j.id||j.task_id||"")})')"
test -n "$task_id"

curl -sS "$api_base/v1/tasks/$task_id" \
  -H "Authorization: Bearer $EVOLINK_API_KEY"
