#!/bin/bash

echo "=== Pitmaster Port & Process Manager ==="
echo
echo "This tool helps you manage ports and processes for Pitmaster."
echo

while true; do
    echo "[MENU] What would you like to do?"
    echo "   1. Check and fix port conflicts (8090, 5173)"
    echo "   2. Kill all PocketBase processes"
    echo "   3. Check current port status"
    echo "   4. Full cleanup (ports + processes)"
    echo "   5. Exit"
    echo
    read -p "Enter your choice (1-5): " choice

    case $choice in
        1)
            echo
            echo "=== Port Conflict Resolution ==="
            echo

            port_8090_in_use=false
            port_5173_in_use=false

            echo "[INFO] Checking for applications using port 8090..."
            if lsof -Pi :8090 -sTCP:LISTEN -t >/dev/null 2>&1; then
                port_8090_in_use=true
                echo "[WARN] Port 8090 is in use by the following processes:"
                lsof -Pi :8090 -sTCP:LISTEN
                echo
            else
                echo "[OK] Port 8090 is available!"
            fi

            echo
            echo "[INFO] Checking for applications using port 5173..."
            if lsof -Pi :5173 -sTCP:LISTEN -t >/dev/null 2>&1; then
                port_5173_in_use=true
                echo "[WARN] Port 5173 is in use by the following processes:"
                lsof -Pi :5173 -sTCP:LISTEN
                echo
            else
                echo "[OK] Port 5173 is available!"
            fi

            echo

            if [ "$port_8090_in_use" = true ]; then
                echo "[QUESTION] Port 8090 is in use. Would you like to:"
                echo "   1. Automatically close processes using port 8090"
                echo "   2. Show process details and close manually"
                echo "   3. Skip port 8090"
                read -p "Enter your choice (1-3): " choice_8090
                
                if [ "$choice_8090" = "1" ]; then
                    echo "[INFO] Attempting to close processes using port 8090..."
                    pids=$(lsof -Pi :8090 -sTCP:LISTEN -t 2>/dev/null)
                    for pid in $pids; do
                        echo "[INFO] Closing process PID: $pid"
                        if kill -9 "$pid" 2>/dev/null; then
                            echo "[OK] Successfully closed process $pid"
                        else
                            echo "[WARN] Could not close process $pid (may require sudo privileges)"
                        fi
                    done
                    echo "[INFO] Port 8090 should now be available."
                    echo
                elif [ "$choice_8090" = "2" ]; then
                    echo "[INFO] Process details for port 8090:"
                    lsof -Pi :8090 -sTCP:LISTEN
                    echo
                    echo "💡 To close manually:"
                    echo "   1. Use 'kill -9 [PID]' to close specific processes"
                    echo "   2. Or close the applications manually"
                    echo
                fi
            fi

            if [ "$port_5173_in_use" = true ]; then
                echo "[QUESTION] Port 5173 is in use. Would you like to:"
                echo "   1. Automatically close processes using port 5173"
                echo "   2. Show process details and close manually"
                echo "   3. Skip port 5173"
                read -p "Enter your choice (1-3): " choice_5173
                
                if [ "$choice_5173" = "1" ]; then
                    echo "[INFO] Attempting to close processes using port 5173..."
                    pids=$(lsof -Pi :5173 -sTCP:LISTEN -t 2>/dev/null)
                    for pid in $pids; do
                        echo "[INFO] Closing process PID: $pid"
                        if kill -9 "$pid" 2>/dev/null; then
                            echo "[OK] Successfully closed process $pid"
                        else
                            echo "[WARN] Could not close process $pid (may require sudo privileges)"
                        fi
                    done
                    echo "[INFO] Port 5173 should now be available."
                    echo
                elif [ "$choice_5173" = "2" ]; then
                    echo "[INFO] Process details for port 5173:"
                    lsof -Pi :5173 -sTCP:LISTEN
                    echo
                    echo "💡 To close manually:"
                    echo "   1. Use 'kill -9 [PID]' to close specific processes"
                    echo "   2. Or close the applications manually"
                    echo
                fi
            fi

            echo "[INFO] Port conflict resolution complete!"
            echo
            read -p "Press Enter to continue..."
            ;;
        2)
            echo
            echo "=== PocketBase Process Management ==="
            echo

            echo "[INFO] Searching for PocketBase processes..."
            echo

            # Find and kill PocketBase processes
            if pgrep -x "pocketbase" > /dev/null; then
                echo "[INFO] Found PocketBase processes. Killing them..."
                pkill -f "pocketbase" 2>/dev/null
                if [ $? -eq 0 ]; then
                    echo "[OK] Successfully killed PocketBase processes"
                else
                    echo "[WARN] Could not kill some PocketBase processes (may require sudo privileges)"
                fi
            else
                echo "[INFO] No PocketBase processes found"
            fi

            echo
            echo "[INFO] Checking if port 8090 is still in use..."
            if lsof -Pi :8090 -sTCP:LISTEN -t >/dev/null 2>&1; then
                echo "[WARN] Port 8090 is still in use by other processes:"
                lsof -Pi :8090 -sTCP:LISTEN
                echo
                echo "[INFO] You may need to manually close these processes"
            else
                echo "[OK] Port 8090 is now available"
            fi

            echo
            echo "[INFO] PocketBase cleanup complete!"
            echo
            read -p "Press Enter to continue..."
            ;;
        3)
            echo
            echo "=== Current Port Status ==="
            echo

            echo "[INFO] Checking port 8090..."
            if lsof -Pi :8090 -sTCP:LISTEN -t >/dev/null 2>&1; then
                echo "[WARN] Port 8090 is in use:"
                lsof -Pi :8090 -sTCP:LISTEN
            else
                echo "[OK] Port 8090 is available"
            fi

            echo
            echo "[INFO] Checking port 5173..."
            if lsof -Pi :5173 -sTCP:LISTEN -t >/dev/null 2>&1; then
                echo "[WARN] Port 5173 is in use:"
                lsof -Pi :5173 -sTCP:LISTEN
            else
                echo "[OK] Port 5173 is available"
            fi

            echo
            echo "[INFO] Checking PocketBase processes..."
            if pgrep -x "pocketbase" > /dev/null; then
                echo "[WARN] PocketBase processes are running:"
                ps aux | grep pocketbase | grep -v grep
            else
                echo "[OK] No PocketBase processes found"
            fi

            echo
            read -p "Press Enter to continue..."
            ;;
        4)
            echo
            echo "=== Full Cleanup ==="
            echo

            echo "[INFO] Starting full cleanup process..."
            echo

            # Kill PocketBase processes
            echo "[INFO] Killing PocketBase processes..."
            if pgrep -x "pocketbase" > /dev/null; then
                pkill -f "pocketbase" 2>/dev/null
                echo "[OK] PocketBase processes killed"
            else
                echo "[INFO] No PocketBase processes found"
            fi

            echo
            # Close processes on ports
            echo "[INFO] Closing processes on port 8090..."
            pids=$(lsof -Pi :8090 -sTCP:LISTEN -t 2>/dev/null)
            for pid in $pids; do
                echo "[INFO] Closing process PID: $pid"
                kill -9 "$pid" 2>/dev/null
            done

            echo "[INFO] Closing processes on port 5173..."
            pids=$(lsof -Pi :5173 -sTCP:LISTEN -t 2>/dev/null)
            for pid in $pids; do
                echo "[INFO] Closing process PID: $pid"
                kill -9 "$pid" 2>/dev/null
            done

            echo
            echo "[INFO] Final status check..."
            echo

            echo "[INFO] Checking port 8090..."
            if lsof -Pi :8090 -sTCP:LISTEN -t >/dev/null 2>&1; then
                echo "[WARN] Port 8090 is still in use:"
                lsof -Pi :8090 -sTCP:LISTEN
            else
                echo "[OK] Port 8090 is now available"
            fi

            echo
            echo "[INFO] Checking port 5173..."
            if lsof -Pi :5173 -sTCP:LISTEN -t >/dev/null 2>&1; then
                echo "[WARN] Port 5173 is still in use:"
                lsof -Pi :5173 -sTCP:LISTEN
            else
                echo "[OK] Port 5173 is now available"
            fi

            echo
            echo "[INFO] Full cleanup complete!"
            echo
            read -p "Press Enter to continue..."
            ;;
        5)
            echo
            echo "[INFO] Exiting Pitmaster Port & Process Manager"
            echo
            exit 0
            ;;
        *)
            echo "[ERROR] Invalid choice. Please try again."
            echo
            ;;
    esac
done

