#!/bin/bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)"
ENV_FILE="$ROOT_DIR/.env"

usage() {
  echo "Usage: $0 {migrate|init-data|bootstrap}"
  echo ""
  echo "Commands:"
  echo "  migrate     Run database migrations only"
  echo "  init-data   Initialize seed data only"
  echo "  bootstrap   Run migrations + init-data"
  echo ""
  echo "Note: Requires Supabase services running (docker compose up -d)"
  exit 1
}

if [ ! -f "$ENV_FILE" ]; then
  echo "Error: env file not found at $ENV_FILE" >&2
  exit 1
fi

set -a
. "$ENV_FILE"
set +a

cd "$ROOT_DIR"

case "${1:-}" in
  migrate)
    echo "=== Running Migrations ==="
    PYTHONPATH=backend/src uv run python -m core.runtime.cli migrate
    ;;
  init-data)
    echo "=== Running Init Data ==="
    PYTHONPATH=backend/src uv run python -m core.runtime.cli init-data
    ;;
  bootstrap)
    echo "=== Running Bootstrap ==="
    PYTHONPATH=backend/src uv run python -m core.runtime.cli bootstrap
    ;;
  *)
    usage
    ;;
esac
