#!/bin/bash
# Verification script for Proxmox workspace setup

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$HOME/.env"
CLAUDE_CONFIG="$HOME/.config/Claude/claude_desktop_config.json"

echo "🔍 Verifying Proxmox Workspace Setup"
echo "===================================="
echo ""

ERRORS=0

# Check 1: pnpm workspace file
echo "1. Checking pnpm-workspace.yaml..."
if [ -f "$SCRIPT_DIR/pnpm-workspace.yaml" ]; then
    echo "   ✅ pnpm-workspace.yaml exists"
else
    echo "   ❌ pnpm-workspace.yaml not found"
    ERRORS=$((ERRORS + 1))
fi

# Check 2: Root package.json
echo ""
echo "2. Checking root package.json..."
if [ -f "$SCRIPT_DIR/package.json" ]; then
    echo "   ✅ package.json exists"
    if grep -q "mcp:start" "$SCRIPT_DIR/package.json"; then
        echo "   ✅ Workspace scripts configured"
    else
        echo "   ⚠️  Workspace scripts may be missing"
    fi
else
    echo "   ❌ package.json not found"
    ERRORS=$((ERRORS + 1))
fi

# Check 3: MCP submodule
echo ""
echo "3. Checking mcp-proxmox submodule..."
if [ -d "$SCRIPT_DIR/mcp-proxmox" ]; then
    echo "   ✅ mcp-proxmox directory exists"
    if [ -f "$SCRIPT_DIR/mcp-proxmox/index.js" ]; then
        echo "   ✅ index.js exists"
    else
        echo "   ❌ index.js not found"
        ERRORS=$((ERRORS + 1))
    fi
    if [ -f "$SCRIPT_DIR/mcp-proxmox/package.json" ]; then
        echo "   ✅ package.json exists"
    else
        echo "   ❌ package.json not found"
        ERRORS=$((ERRORS + 1))
    fi
else
    echo "   ❌ mcp-proxmox directory not found"
    echo "   💡 Run: git submodule update --init --recursive"
    ERRORS=$((ERRORS + 1))
fi

# Check 4: Frontend submodule
echo ""
echo "4. Checking ProxmoxVE/frontend submodule..."
if [ -d "$SCRIPT_DIR/ProxmoxVE/frontend" ]; then
    echo "   ✅ ProxmoxVE/frontend directory exists"
    if [ -f "$SCRIPT_DIR/ProxmoxVE/frontend/package.json" ]; then
        echo "   ✅ package.json exists"
    else
        echo "   ❌ package.json not found"
        ERRORS=$((ERRORS + 1))
    fi
else
    echo "   ⚠️  ProxmoxVE/frontend directory not found (optional)"
fi

# Check 5: Dependencies installed
echo ""
echo "5. Checking dependencies..."
if [ -d "$SCRIPT_DIR/node_modules" ] || [ -d "$SCRIPT_DIR/mcp-proxmox/node_modules" ]; then
    echo "   ✅ Dependencies appear to be installed"
    echo "   💡 Run: pnpm install (if unsure)"
else
    echo "   ⚠️  Dependencies may not be installed"
    echo "   💡 Run: pnpm install"
fi

# Check 6: .env file
echo ""
echo "6. Checking .env configuration..."
if [ -f "$ENV_FILE" ]; then
    echo "   ✅ .env file exists at $ENV_FILE"
    
    # Check for required variables
    MISSING_VARS=0
    if ! grep -q "^PROXMOX_HOST=" "$ENV_FILE" 2>/dev/null || grep -q "^PROXMOX_HOST=your-proxmox" "$ENV_FILE" 2>/dev/null; then
        echo "   ⚠️  PROXMOX_HOST not configured"
        MISSING_VARS=$((MISSING_VARS + 1))
    fi
    if ! grep -q "^PROXMOX_TOKEN_VALUE=" "$ENV_FILE" 2>/dev/null || grep -q "^PROXMOX_TOKEN_VALUE=your-token" "$ENV_FILE" 2>/dev/null; then
        echo "   ⚠️  PROXMOX_TOKEN_VALUE not configured"
        MISSING_VARS=$((MISSING_VARS + 1))
    fi
    
    if [ $MISSING_VARS -eq 0 ]; then
        echo "   ✅ Required environment variables appear configured"
    else
        echo "   ⚠️  Some environment variables need to be configured"
    fi
else
    echo "   ⚠️  .env file not found at $ENV_FILE"
    echo "   💡 Run: ./setup.sh or copy .env.example to $ENV_FILE"
fi

# Check 7: Claude Desktop config
echo ""
echo "7. Checking Claude Desktop configuration..."
if [ -f "$CLAUDE_CONFIG" ]; then
    echo "   ✅ Claude Desktop config exists"
    if grep -q "proxmox" "$CLAUDE_CONFIG" 2>/dev/null; then
        echo "   ✅ Proxmox MCP server configured"
    else
        echo "   ⚠️  Proxmox MCP server may not be configured"
    fi
else
    echo "   ⚠️  Claude Desktop config not found"
    echo "   💡 Run: ./setup.sh"
fi

# Check 8: Node.js and pnpm
echo ""
echo "8. Checking Node.js and pnpm..."
if command -v node &> /dev/null; then
    NODE_VERSION=$(node --version)
    echo "   ✅ Node.js installed: $NODE_VERSION"
    
    # Check version
    NODE_MAJOR=$(echo "$NODE_VERSION" | cut -d'.' -f1 | sed 's/v//')
    if [ "$NODE_MAJOR" -ge 16 ]; then
        echo "   ✅ Node.js version 16+ (required)"
    else
        echo "   ⚠️  Node.js version should be 16+"
        ERRORS=$((ERRORS + 1))
    fi
else
    echo "   ❌ Node.js not found"
    ERRORS=$((ERRORS + 1))
fi

if command -v pnpm &> /dev/null; then
    PNPM_VERSION=$(pnpm --version)
    echo "   ✅ pnpm installed: $PNPM_VERSION"
else
    echo "   ❌ pnpm not found"
    echo "   💡 Install with: npm install -g pnpm"
    ERRORS=$((ERRORS + 1))
fi

# Summary
echo ""
echo "===================================="
if [ $ERRORS -eq 0 ]; then
    echo "✅ Setup verification complete - no critical errors found!"
    echo ""
    echo "Next steps:"
    echo "  • Test MCP server: pnpm test:basic"
    echo "  • Start MCP server: pnpm mcp:start"
    echo "  • Start frontend: pnpm frontend:dev"
    exit 0
else
    echo "⚠️  Found $ERRORS critical error(s)"
    echo ""
    echo "Please fix the errors above before proceeding."
    exit 1
fi

