#!/bin/bash

# LuckyChit Diagnostic Script
# Checks everything and reports issues

echo "🔍 LuckyChit Diagnostic Report"
echo "==============================="
echo ""

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

# Port Check
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 ""

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

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

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

# Memory
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "6️⃣  Memory Usage"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
free -h
echo ""

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

# Recent Errors
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "8️⃣  Recent PM2 Errors"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
pm2 logs --err --lines 10 --nostream 2>/dev/null || echo "No recent errors"
echo ""

# Summary
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📋 SUMMARY & RECOMMENDATIONS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

PM2_COUNT=$(pm2 jlist 2>/dev/null | grep -c '"pm2_env"')
if [ "$PM2_COUNT" -lt 2 ]; then
    echo "⚠️  PM2 processes missing or down"
    echo "   FIX: pm2 restart all  OR  ./scripts/fix-502.sh"
fi

if [ -z "$PORTS" ]; then
    echo "⚠️  No processes listening on required ports"
    echo "   FIX: ./scripts/fix-502.sh"
fi

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

if [ "$FRONTEND" != "200" ]; then
    echo "⚠️  Frontend not responding"
    echo "   FIX: pm2 restart luckychit-frontend"
fi

if [ "$POSTGRES" != "active" ]; then
    echo "⚠️  PostgreSQL not running"
    echo "   FIX: sudo systemctl start postgresql"
fi

echo ""
echo "📚 See TROUBLESHOOTING.md for detailed fixes"

