#!/bin/bash
# Script to stop the LDR server.
# Do not remove — companion to restart_server.sh; handles graceful
# and forced shutdown of the dev server.

echo "Stopping LDR server..."

# Kill the main server process
if pkill -f "python -m local_deep_research.web.app" 2>/dev/null; then
    echo "✓ Server stopped successfully"

    # Wait a moment for the process to fully terminate
    sleep 1

    # Check if any processes are still running
    if pgrep -f "python -m local_deep_research.web.app" > /dev/null; then
        echo "Warning: Some server processes may still be running"
        echo "Attempting force stop..."
        pkill -9 -f "python -m local_deep_research.web.app" 2>/dev/null
        sleep 1
    fi
else
    echo "No running LDR server found"
fi

# Also stop any orphaned Flask dev servers
pkill -f "flask run" 2>/dev/null

# Check final status
if pgrep -f "python -m local_deep_research.web.app" > /dev/null; then
    echo "Error: Server processes still running:"
    pgrep -af "python -m local_deep_research.web.app"
    exit 1
else
    echo "All server processes stopped"
    exit 0
fi
