#!/bin/bash

# UrbanGamers VPS Diagnostic Script
# This will help identify why the app isn't running

echo "🔍 Starting diagnostic check..."
echo ""

# Find project directory
PROJECT_DIR=$(find /var/www /home -name "package.json" -type f 2>/dev/null | grep -v node_modules | head -1 | xargs dirname)

if [ -z "$PROJECT_DIR" ]; then
    echo "❌ Error: Could not find project directory"
    exit 1
fi

echo "📁 Project directory: $PROJECT_DIR"
cd "$PROJECT_DIR"
echo ""

# 1. Check Node.js version
echo "1️⃣ Checking Node.js version..."
node -v || echo "❌ Node.js not found"
npm -v || echo "❌ npm not found"
echo ""

# 2. Check if dependencies are installed
echo "2️⃣ Checking critical dependencies..."
if [ ! -d "node_modules/next-intl" ]; then
    echo "❌ next-intl not installed!"
    echo "   Run: npm install next-intl@latest @formatjs/intl-localematcher --save"
fi
if [ ! -d "node_modules/class-variance-authority" ]; then
    echo "❌ class-variance-authority not installed!"
fi
if [ ! -d "node_modules/clsx" ]; then
    echo "❌ clsx not installed!"
fi
if [ ! -d "node_modules/tailwind-merge" ]; then
    echo "❌ tailwind-merge not installed!"
fi
if [ ! -d "node_modules/bcryptjs" ]; then
    echo "❌ bcryptjs not installed!"
fi
if [ ! -d "node_modules/tailwindcss-animate" ]; then
    echo "❌ tailwindcss-animate not installed!"
fi
echo "✅ Dependency check complete"
echo ""

# 3. Check if build exists
echo "3️⃣ Checking build..."
if [ ! -f ".next/BUILD_ID" ]; then
    echo "❌ Build not found! Need to run: npm run build"
else
    echo "✅ Build exists: $(cat .next/BUILD_ID)"
fi
echo ""

# 4. Check PM2 status
echo "4️⃣ Checking PM2 processes..."
pm2 list
echo ""

# 5. Check PM2 error logs
echo "5️⃣ Recent error logs (last 50 lines):"
pm2 logs urbangamers --err --lines 50 --nostream 2>&1 | tail -30
echo ""

# 6. Check PM2 output logs
echo "6️⃣ Recent output logs (last 50 lines):"
pm2 logs urbangamers --out --lines 50 --nostream 2>&1 | tail -30
echo ""

# 7. Test if port 3000 is in use
echo "7️⃣ Checking port 3000..."
if netstat -tuln 2>/dev/null | grep -q ":3000 "; then
    echo "✅ Port 3000 is in use"
    netstat -tuln | grep ":3000 "
else
    echo "❌ Port 3000 is NOT in use - app is not running!"
fi
echo ""

# 8. Check if can manually start
echo "8️⃣ Testing manual start (will timeout after 5 seconds)..."
timeout 5 npm start 2>&1 | head -20 || echo "Process timed out or failed"
echo ""

# 9. Check environment file
echo "9️⃣ Checking .env.local..."
if [ -f ".env.local" ]; then
    echo "✅ .env.local exists"
    # Check for critical variables
    if grep -q "DATABASE_" .env.local; then
        echo "✅ Database config found"
    else
        echo "⚠️  Database config might be missing"
    fi
else
    echo "❌ .env.local not found!"
fi
echo ""

# 10. Check Nginx
echo "🔟 Checking Nginx..."
systemctl status nginx --no-pager | head -10
echo ""

echo "✅ Diagnostic complete!"
echo ""
echo "📋 Next steps:"
echo "1. If dependencies missing: npm install"
echo "2. If build missing: npm run build"
echo "3. If PM2 errors found: Check logs above"
echo "4. Restart PM2: pm2 restart all"

