#!/bin/bash

echo "========================================"
echo "Starting AI-CMO Development Servers"
echo "========================================"
echo ""

echo "[1/3] Clearing corrupted .next directories..."
cd "$(dirname "$0")"

if [ -d "src/frontend/.next" ]; then
    echo "Deleting src/frontend/.next..."
    rm -rf src/frontend/.next
fi

if [ -d "src/frontend/node_modules/.cache" ]; then
    echo "Deleting src/frontend/node_modules/.cache..."
    rm -rf src/frontend/node_modules/.cache
fi

echo "[OK] Build directories cleared"
echo ""

echo "[2/3] Starting Backend Server (port 3101)..."
cd src/backend
npm run dev &
BACKEND_PID=$!
cd ../..

sleep 3

echo "[3/3] Starting Frontend Server (port 3100)..."
cd src/frontend
npm run dev &
FRONTEND_PID=$!
cd ../..

echo ""
echo "========================================"
echo "Both servers started!"
echo "Backend:  http://localhost:3101 (PID: $BACKEND_PID)"
echo "Frontend: http://localhost:3100 (PID: $FRONTEND_PID)"
echo "========================================"
echo ""
echo "Press Ctrl+C to stop both servers..."

wait