#!/bin/bash

# Kill any processes running on ports 3000 and 3001
echo "🔍 Checking for processes on ports 3000 and 3001..."
PIDS=$(lsof -ti:3000,3001 2>/dev/null)
if [ -n "$PIDS" ]; then
  echo "$PIDS" | xargs kill -9 2>/dev/null
  echo "✅ Killed processes on ports 3000/3001"
else
  echo "ℹ️  No processes found on ports 3000/3001"
fi

# Kill any old Next.js dev server processes (but not the current script)
echo "🔍 Checking for Next.js dev processes..."
OLD_PIDS=$(ps aux | grep -i "next dev" | grep -v grep | grep -v "kill-dev.sh" | awk '{print $2}')
if [ -n "$OLD_PIDS" ]; then
  echo "$OLD_PIDS" | xargs kill -9 2>/dev/null
  echo "✅ Killed Next.js dev processes"
else
  echo "ℹ️  No Next.js dev processes found"
fi

# Give it a moment to clean up
sleep 0.5

echo "✨ Ready to start fresh dev server!"
