#!/bin/bash

echo "╔══════════════════════════════════════════════════╗"
echo "║  Localhost Verification - Sky Art Shop          ║"
echo "╚══════════════════════════════════════════════════╝"
echo ""

echo "🔍 Checking Active Ports..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Check port 80
if curl -s http://localhost/ >/dev/null 2>&1; then
    echo "❌ Port 80 (http://localhost/) - ACTIVE (Should be disabled!)"
    echo "   Run: sudo systemctl stop nginx apache2"
else
    echo "✅ Port 80 (http://localhost/) - Not accessible (Correct!)"
fi

# Check port 5000
if curl -s http://localhost:5000/ >/dev/null 2>&1; then
    echo "✅ Port 5000 (http://localhost:5000/) - ACTIVE (Correct!)"
    TITLE=$(curl -s http://localhost:5000/ | grep -o "<title>[^<]*</title>" | head -1)
    echo "   Serving: $TITLE"
else
    echo "❌ Port 5000 (http://localhost:5000/) - Not accessible (Should be running!)"
fi

echo ""
echo "🌐 Web Servers Status..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Check nginx
if systemctl is-active --quiet nginx; then
    echo "⚠️  Nginx: RUNNING (Should be stopped for development)"
else
    echo "✅ Nginx: Stopped"
fi

# Check apache
if systemctl is-active --quiet apache2; then
    echo "⚠️  Apache: RUNNING (Should be stopped)"
else
    echo "✅ Apache: Stopped"
fi

# Check PM2
if pm2 list | grep -q "skyartshop.*online"; then
    echo "✅ PM2 Backend: Running"
else
    echo "❌ PM2 Backend: Not running"
fi

echo ""
echo "📊 Summary:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Count active ports
ACTIVE_PORTS=$(ss -tln | grep -E ":80 |:443 |:5000 " | wc -l)

if [ "$ACTIVE_PORTS" -eq 1 ]; then
    echo "✅ Perfect! Only 1 web port active (port 5000)"
    echo "   Your site: http://localhost:5000"
else
    echo "⚠️  Multiple web ports detected: $ACTIVE_PORTS"
    echo "   Active ports:"
    ss -tln | grep -E ":80 |:443 |:5000 "
fi

echo ""
