#!/usr/bin/env bash
set -e

ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"

cleanup() {
  echo ""
  echo "▶ 종료 중..."
  kill $BACKEND_PID $FRONTEND_PID 2>/dev/null
  wait $BACKEND_PID $FRONTEND_PID 2>/dev/null
  echo "▶ 종료 완료"
  exit 0
}
trap cleanup SIGINT SIGTERM

echo "▶ 백엔드 시작 (uvicorn, port 8000)"
cd "$ROOT_DIR/backend"
uvicorn app:app --reload --port 8000 &
BACKEND_PID=$!

echo "▶ 프론트엔드 시작 (vite dev, port 5173)"
cd "$ROOT_DIR/frontend"
npm run dev &
FRONTEND_PID=$!

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  백엔드: http://localhost:8000"
echo "  프론트엔드: http://localhost:5173"
echo "  종료: Ctrl+C"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

wait $BACKEND_PID $FRONTEND_PID
