#!/bin/bash

echo "🛑 Stopping Orchestry..."
echo ""

# Kill processes on port 3001 (API server)
if lsof -ti :3001 > /dev/null 2>&1; then
    echo "Stopping API server on port 3001..."
    lsof -ti :3001 | xargs kill -9 2>/dev/null
    echo "✅ Stopped API server"
fi

# Kill processes on port 3000 (Vite dev server)
if lsof -ti :3000 > /dev/null 2>&1; then
    echo "Stopping Web UI on port 3000..."
    lsof -ti :3000 | xargs kill -9 2>/dev/null
    echo "✅ Stopped Web UI"
fi

# Kill any node processes related to bmad-task-manager
if pgrep -f "bmad-task-manager" > /dev/null 2>&1; then
    echo "Stopping Orchestry processes..."
    pkill -f "bmad-task-manager"
    echo "✅ Stopped Orchestry processes"
fi

# Kill Vite dev server if running
if pgrep -f "vite" > /dev/null 2>&1; then
    echo "Stopping Vite dev server..."
    pkill -f "vite"
    echo "✅ Stopped Vite server"
fi

echo ""
echo "✅ All Orchestry services stopped"