#!/usr/bin/env bash
# Temporary dev script for ERP migration — starts NocoDB + Express server + Vite

set -e

cleanup() {
  echo ""
  echo "Shutting down..."
  kill $SERVER_PID $CLIENT_PID 2>/dev/null
  docker stop nocodb 2>/dev/null
  echo "Done."
}

trap cleanup EXIT INT TERM

cd "$(dirname "$0")"

# Start NocoDB
if docker ps --format '{{.Names}}' | grep -q '^nocodb$'; then
  echo "NocoDB already running on port 8090"
else
  echo "Starting NocoDB..."
  docker start nocodb 2>/dev/null || docker run -d \
    --name nocodb -p 8090:8080 nocodb/nocodb:latest
fi

echo "Waiting for NocoDB..."
for i in $(seq 1 30); do
  curl -s http://localhost:8090/api/v1/health >/dev/null 2>&1 && echo "NocoDB ready" && break
  sleep 1
done

# Start Express server (port 3002)
echo "Starting Express server..."
(cd server && npm run dev) &
SERVER_PID=$!

sleep 2

# Start Vite (port 3000)
echo "Starting Vite..."
npx vite &
CLIENT_PID=$!

wait $CLIENT_PID
