#!/bin/bash

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# VPN Configuration
VPN_CONFIG="$HOME/wg1.conf"
VPN_NAME="Proxmox Colocation VPN"
VPN_IP="10.6.0.3"
VPN_NETWORK="10.6.0.0/24, 10.4.0.0/24"

# Function to get active VPN interface
get_active_vpn() {
    sudo wg show interfaces 2>/dev/null
}

# Function to show detailed status
show_status() {
    echo "🔍 Checking VPN connection..."
    echo ""
    
    local active_interface=$(get_active_vpn)
    
    if [ -z "$active_interface" ]; then
        echo "🔴 VPN not connected"
        return
    fi
    
    echo "🟢 VPN Connected:"
    echo ""
    sudo wg show
    echo ""
    
    # Show routing information
    echo "📡 Network Routes:"
    netstat -rn | grep -E "10\.(4|6)\.0" | awk '{printf "   %-20s -> %s\n", $1, $NF}'
}

# Function to connect VPN
connect_vpn() {
    if [ ! -f "$VPN_CONFIG" ]; then
        echo "❌ Configuration file not found: $VPN_CONFIG"
        return 1
    fi
    
    echo "🔄 Connecting to $VPN_NAME..."
    result=$(sudo wg-quick up "$VPN_CONFIG" 2>&1)
    
    if [ $? -eq 0 ]; then
        echo "✅ Connected Successfully!"
        echo "   VPN IP: $VPN_IP"
        echo "   Networks: $VPN_NETWORK"
        sleep 1
        return 0
    else
        if echo "$result" | grep -q "already exists"; then
            echo "ℹ️  VPN is already connected"
            return 0
        else
            echo "❌ Failed to connect:"
            echo "$result" | head -5
            return 1
        fi
    fi
}

# Function to disconnect VPN
disconnect_vpn() {
    if [ ! -f "$VPN_CONFIG" ]; then
        echo "❌ Configuration file not found: $VPN_CONFIG"
        return 1
    fi
    
    echo "🔄 Disconnecting $VPN_NAME..."
    result=$(sudo wg-quick down "$VPN_CONFIG" 2>&1)
    
    if [ $? -eq 0 ]; then
        echo "✅ Disconnected Successfully!"
        return 0
    else
        if echo "$result" | grep -q "is not a WireGuard interface"; then
            echo "ℹ️  VPN is not connected"
            return 0
        else
            echo "❌ Failed to disconnect:"
            echo "$result" | head -5
            return 1
        fi
    fi
}

# Function to test connectivity
test_connectivity() {
    local active_interface=$(get_active_vpn)
    
    if [ -z "$active_interface" ]; then
        echo "❌ VPN not connected. Please connect first."
        return
    fi
    
    echo "🔍 Testing connectivity..."
    echo ""
    
    # Test internal network (10.4.0.0/24)
    echo "Testing Internal Network (10.4.0.0/24):"
    if ping -c 1 -W 2 10.4.0.1 >/dev/null 2>&1; then
        echo "  ✅ Gateway (10.4.0.1): Reachable"
    else
        echo "  ⚠️  Gateway (10.4.0.1): Not reachable"
    fi
    
    if ping -c 1 -W 2 10.4.0.148 >/dev/null 2>&1; then
        echo "  ✅ Proxmox Host (10.4.0.148): Reachable"
    else
        echo "  ⚠️  Proxmox Host (10.4.0.148): Not reachable"
    fi
    
    if ping -c 1 -W 2 10.4.0.205 >/dev/null 2>&1; then
        echo "  ✅ DeafGain Server (10.4.0.205): Reachable"
    else
        echo "  ⚠️  DeafGain Server (10.4.0.205): Not reachable"
    fi
}

# Clear screen and show header
clear
echo "=================================================="
echo "🔐 WireGuard VPN Manager - Proxmox Colocation"
echo "=================================================="
echo ""

# Main menu
while true; do
    # Check VPN status
    active_interface=$(get_active_vpn)
    
    if [ -n "$active_interface" ]; then
        echo "🟢 Status: VPN Connected"
    else
        echo "🔴 Status: VPN Disconnected"
    fi
    echo ""
    
    echo "Options:"
    echo "1) Connect to VPN"
    echo "2) Disconnect from VPN"
    echo "3) Show Detailed Status"
    echo "4) Test Connectivity"
    echo "5) Quit"
    echo ""
    read -p "Enter your choice (1-5): " choice
    
    case $choice in
        1)
            echo ""
            connect_vpn
            ;;
        2)
            echo ""
            disconnect_vpn
            ;;
        3)
            echo ""
            show_status
            ;;
        4)
            echo ""
            test_connectivity
            ;;
        5)
            echo ""
            echo "👋 Goodbye!"
            exit 0
            ;;
        *)
            echo ""
            echo "❌ Invalid choice. Please enter 1-5."
            ;;
    esac
    
    echo ""
    echo "=================================================="
    echo ""
done
