#!/bin/bash
set -euo pipefail
# Quick dev script - just starts the mock backend for UI development

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
FRONTEND_DIR="$PROJECT_ROOT/neode-ui"

if [ ! -d "$FRONTEND_DIR" ]; then
    echo "❌ Frontend directory not found: $FRONTEND_DIR"
    exit 1
fi

echo "📁 Using: $FRONTEND_DIR"
cd "$FRONTEND_DIR"

# Kill any existing processes on ports 5959 and 8100
echo "🧹 Cleaning up ports 5959 and 8100..."
lsof -ti:5959 | xargs kill -9 2>/dev/null || true
lsof -ti:8100 | xargs kill -9 2>/dev/null || true
sleep 1

# Check if node_modules exists
if [ ! -d "node_modules" ]; then
    echo "⚠️  Installing dependencies..."
    npm install
fi

# Check if mock-backend.js exists
if [ ! -f "mock-backend.js" ]; then
    echo "❌ mock-backend.js not found in $FRONTEND_DIR"
    echo "   Make sure you're using the correct project directory"
    exit 1
fi

echo "🚀 Starting frontend with mock backend..."
echo "   (Press Ctrl+C to stop)"
echo ""
npm run dev:mock
