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

# Local development helper (Docker-first).
# - mock mode: no ComfyUI / no OpenAI key required (uses fallback scenes)
# - comfy mode: assumes ComfyUI is running on host at :8188

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"

MODE="${1:-mock}"         # mock | comfy
PROMPT="${2:-写一个温暖的城市夜景故事}"

CONFIG_PATH="${CONFIG_PATH:-./configs/config.yaml}"

echo "[dev] building image..."
docker compose build

if [[ "$MODE" == "mock" ]]; then
  echo "[dev] run mock pipeline..."
  docker compose run --rm \
    -e OPENAI_API_KEY="${OPENAI_API_KEY:-}" \
    -e OPENAI_BASE_URL="${OPENAI_BASE_URL:-}" \
    aivideo \
    python main.py --config "$CONFIG_PATH" --prompt "$PROMPT" --mock
  echo "[dev] done. output: ./final_poc.mp4"
  exit 0
fi

if [[ "$MODE" == "comfy" ]]; then
  echo "[dev] checking ComfyUI connectivity (host.docker.internal)..."
  docker compose run --rm aivideo python scripts/check_comfy.py --base-url http://host.docker.internal:8188 >/dev/null
  echo "[dev] run comfy pipeline..."
  echo "[dev] NOTE: set app.comfy_base_url=http://host.docker.internal:8188 in configs/config.yaml"
  docker compose run --rm \
    -e OPENAI_API_KEY="${OPENAI_API_KEY:-}" \
    -e OPENAI_BASE_URL="${OPENAI_BASE_URL:-}" \
    aivideo \
    python main.py --config "$CONFIG_PATH" --prompt "$PROMPT"
  echo "[dev] done. output: ./final_poc.mp4"
  exit 0
fi

echo "[dev] unknown mode: $MODE (use: mock | comfy)" >&2
exit 2

