#!/bin/bash

# 502 Error Diagnostic Script
# Run this on production server to diagnose Bad Gateway issues

echo "🔍 LuckyChit 502 Error Diagnostic"
echo "===================================="
echo ""
echo "Checking server status..."
echo ""

# 1. PM2 Status
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "1️⃣  PM2 Process Status"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
pm2 status

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "2️⃣  Port Listening Check"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
PORTS=$(netstat -tulpn 2>/dev/null | grep -E '(3000|8080)')
if [ -z "$PORTS" ]; then
    echo "❌ No processes listening on ports 3000 or 8080"
else
    echo "✅ Ports are listening:"
    echo "$PORTS"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "3️⃣  Backend Health Check (localhost)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
BACKEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/health 2>/dev/null)
if [ "$BACKEND_RESPONSE" = "200" ]; then
    echo "✅ Backend responding: HTTP $BACKEND_RESPONSE"
    curl -s http://localhost:3000/health | head -n 5
else
    echo "❌ Backend not responding (HTTP $BACKEND_RESPONSE)"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "4️⃣  Frontend Health Check (localhost)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
FRONTEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080 2>/dev/null)
if [ "$FRONTEND_RESPONSE" = "200" ]; then
    echo "✅ Frontend responding: HTTP $FRONTEND_RESPONSE"
else
    echo "❌ Frontend not responding (HTTP $FRONTEND_RESPONSE)"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "5️⃣  External Access Check (192.168.8.148)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
EXTERNAL_BACKEND=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:3000/health 2>/dev/null)
if [ "$EXTERNAL_BACKEND" = "200" ]; then
    echo "✅ Backend accessible from IP: HTTP $EXTERNAL_BACKEND"
else
    echo "❌ Backend not accessible from IP (HTTP $EXTERNAL_BACKEND)"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "6️⃣  Firewall Status"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
UFW_STATUS=$(sudo ufw status 2>/dev/null | grep -E '(3000|8080|80|443)')
if [ -z "$UFW_STATUS" ]; then
    echo "⚠️  Firewall ports may not be open"
else
    echo "✅ Firewall rules:"
    echo "$UFW_STATUS"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "7️⃣  Disk Space"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
df -h | grep -E '(Filesystem|/$|/home)'

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "8️⃣  Memory Usage"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
free -h

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "9️⃣  PostgreSQL Status"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
POSTGRES_STATUS=$(sudo systemctl is-active postgresql 2>/dev/null)
if [ "$POSTGRES_STATUS" = "active" ]; then
    echo "✅ PostgreSQL is running"
else
    echo "❌ PostgreSQL is not running"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🔟  Recent PM2 Error Logs"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
pm2 logs --err --lines 10 --nostream 2>/dev/null || echo "No recent errors"

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Diagnosis Complete!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Summary and recommendations
echo "📋 SUMMARY & RECOMMENDATIONS:"
echo ""

# Check PM2 status
PM2_COUNT=$(pm2 jlist 2>/dev/null | grep -c '"pm2_env"')
if [ "$PM2_COUNT" -lt 2 ]; then
    echo "⚠️  ISSUE: PM2 processes are missing or down"
    echo "   FIX: pm2 restart all  OR  recreate processes"
    echo ""
fi

# Check ports
if [ -z "$PORTS" ]; then
    echo "⚠️  ISSUE: No processes listening on required ports"
    echo "   FIX: Start PM2 processes"
    echo ""
fi

# Check backend
if [ "$BACKEND_RESPONSE" != "200" ]; then
    echo "⚠️  ISSUE: Backend not responding"
    echo "   FIX: pm2 restart luckychit-api"
    echo "   CHECK: pm2 logs luckychit-api"
    echo ""
fi

# Check frontend
if [ "$FRONTEND_RESPONSE" != "200" ]; then
    echo "⚠️  ISSUE: Frontend not responding"
    echo "   FIX: pm2 restart luckychit-frontend"
    echo "   CHECK: pm2 logs luckychit-frontend"
    echo ""
fi

# Check PostgreSQL
if [ "$POSTGRES_STATUS" != "active" ]; then
    echo "⚠️  ISSUE: PostgreSQL is not running"
    echo "   FIX: sudo systemctl start postgresql"
    echo ""
fi

echo "📚 For detailed fixes, see: FIX_502_ERROR.md"
echo ""

