#!/bin/bash
# Check status of TechZone applications

echo "╔════════════════════════════════════════════════════════╗"
echo "║       TechZone Application Status                     ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""

# PM2 Status
if command -v pm2 &> /dev/null; then
    echo "📊 PM2 Process Status:"
    pm2 status
    echo ""
fi

# HTTP Status
echo "🌐 HTTP Connectivity:"
BACKEND_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8181/api/health 2>/dev/null || echo "000")
FRONTEND_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5300 2>/dev/null || echo "000")

if [ "$BACKEND_STATUS" = "200" ]; then
    echo "   ✅ Backend:  HTTP $BACKEND_STATUS - http://localhost:8181"
else
    echo "   ❌ Backend:  HTTP $BACKEND_STATUS - NOT RESPONDING"
fi

if [ "$FRONTEND_STATUS" = "200" ]; then
    echo "   ✅ Frontend: HTTP $FRONTEND_STATUS - http://localhost:5300"
else
    echo "   ❌ Frontend: HTTP $FRONTEND_STATUS - NOT RESPONDING"
fi

echo ""

# Database test
echo "🗄️  Database Connectivity:"
cd /media/pts/Website/PromptTech_Solution_Site/backend
if [ -f "venv/bin/python" ]; then
    source venv/bin/activate
    python -c "
from database import engine
from sqlalchemy import text
try:
    conn = engine.connect()
    result = conn.execute(text('SELECT 1'))
    print('   ✅ Database: Connected')
    conn.close()
except Exception as e:
    print(f'   ❌ Database: Error - {e}')
" 2>/dev/null
else
    echo "   ⚠️  Virtual environment not found"
fi

echo ""
echo "📝 View logs:"
echo "   pm2 logs techzone-backend --lines 50"
echo "   pm2 logs techzone-frontend --lines 50"
echo ""
