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

echo "[quickstart] Building and starting containers..."
docker compose up --build -d

echo "[quickstart] Waiting for backend health..."
for i in {1..30}; do
  if curl -sf http://localhost:8000/health >/dev/null; then
    echo "[quickstart] Backend healthy."
    break
  fi
  sleep 2
done

echo "[quickstart] Verifying API chain (inspiration → outline → chapter → global-lines)..."
insp=$(curl -s -X POST http://localhost:8000/api/inspiration \
  -H 'Content-Type: application/json' \
  -d '{"title":"快速验证","description":"用于 quickstart 的最小灵感描述","target_chapters":3}')
novel_id=$(echo "$insp" | jq -r .id)

outline=$(curl -s -X POST http://localhost:8000/api/outline \
  -H 'Content-Type: application/json' \
  -d "{\"inspiration_id\":\"$novel_id\",\"target_chapters\":3,\"complexity\":\"medium\"}")
outline_id=$(echo "$outline" | jq -r .id)

chapter=$(curl -s -X POST http://localhost:8000/api/novel/$novel_id/chapter \
  -H 'Content-Type: application/json' \
  -d "{\"outline_id\":\"$outline_id\",\"chapter_number\":1}")

gl=$(curl -s http://localhost:8000/api/novel/$novel_id/global-lines)

echo "[quickstart] Inspiration ID: $novel_id"
echo "[quickstart] Outline ID:     $outline_id"
echo "[quickstart] Chapter length:  $(echo "$chapter" | jq -r .content | wc -c)"
echo "[quickstart] GlobalLines ts:  $(echo "$gl" | jq -r .updated_at)"

echo "[quickstart] Frontend should be available at: http://localhost:3000"
echo "[quickstart] Done."

