#!/bin/bash
# Complete System Verification Script for Church Music Database
# Run this to verify everything is working correctly

echo "═══════════════════════════════════════════════════"
echo "  CHURCH MUSIC DATABASE - SYSTEM VERIFICATION"
echo "═══════════════════════════════════════════════════"
echo ""

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Check ports
echo "📍 PORT VERIFICATION:"
PORT_8080=$(lsof -ti:8080 2>/dev/null | wc -l)
PORT_5100=$(lsof -ti:5100 2>/dev/null | wc -l)

if [ "$PORT_8080" -gt 0 ]; then
    echo -e "  ${GREEN}✅ Port 8080 (Backend):${NC} Active"
else
    echo -e "  ${RED}❌ Port 8080 (Backend):${NC} NOT RUNNING"
fi

if [ "$PORT_5100" -gt 0 ]; then
    echo -e "  ${GREEN}✅ Port 5100 (Frontend):${NC} Active"
else
    echo -e "  ${RED}❌ Port 5100 (Frontend):${NC} NOT RUNNING"
fi

echo ""

# Check systemd services
echo "🔧 SYSTEMD SERVICES:"
BACKEND_STATUS=$(systemctl is-active church-music-backend)
FRONTEND_STATUS=$(systemctl is-active church-music-frontend)

if [ "$BACKEND_STATUS" = "active" ]; then
    echo -e "  ${GREEN}✅ Backend Service:${NC} $BACKEND_STATUS"
else
    echo -e "  ${RED}❌ Backend Service:${NC} $BACKEND_STATUS"
fi

if [ "$FRONTEND_STATUS" = "active" ]; then
    echo -e "  ${GREEN}✅ Frontend Service:${NC} $FRONTEND_STATUS"
else
    echo -e "  ${RED}❌ Frontend Service:${NC} $FRONTEND_STATUS"
fi

echo ""

# Test API endpoints
echo "🌐 API TESTS:"

# Test Backend
BACKEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/api/songs 2>/dev/null)
if [ "$BACKEND_RESPONSE" = "200" ]; then
    SONG_COUNT=$(curl -s http://localhost:8080/api/songs 2>/dev/null | jq '. | length' 2>/dev/null || echo "?")
    echo -e "  ${GREEN}✅ Backend API (8080):${NC} HTTP $BACKEND_RESPONSE ($SONG_COUNT songs)"
else
    echo -e "  ${RED}❌ Backend API (8080):${NC} HTTP $BACKEND_RESPONSE"
fi

# Test Frontend
FRONTEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5100 2>/dev/null)
if [ "$FRONTEND_RESPONSE" = "200" ]; then
    echo -e "  ${GREEN}✅ Frontend (5100):${NC} HTTP $FRONTEND_RESPONSE"
else
    echo -e "  ${RED}❌ Frontend (5100):${NC} HTTP $FRONTEND_RESPONSE"
fi

# Test HTTPS
HTTPS_RESPONSE=$(curl -k -s -o /dev/null -w "%{http_code}" https://houseofprayer.ddns.net 2>/dev/null)
if [ "$HTTPS_RESPONSE" = "200" ]; then
    echo -e "  ${GREEN}✅ HTTPS Site:${NC} HTTP $HTTPS_RESPONSE"
else
    echo -e "  ${RED}❌ HTTPS Site:${NC} HTTP $HTTPS_RESPONSE"
fi

echo ""

# Check database
echo "💾 DATABASE:"
DB_CHECK=$(cd /media/pts/Website/Church_HOP_MusicData/backend && source venv/bin/activate && python3 -c "from postgresql_models import SessionLocal; db = SessionLocal(); print('ok'); db.close()" 2>/dev/null)
if [ "$DB_CHECK" = "ok" ]; then
    echo -e "  ${GREEN}✅ PostgreSQL Connection:${NC} Working"
else
    echo -e "  ${RED}❌ PostgreSQL Connection:${NC} Failed"
fi

echo ""

# Check for rogue processes
echo "⚠️  ROGUE PROCESS CHECK:"
ROGUE_PORTS=$(lsof -i -P -n | grep LISTEN | grep -E ":(3000|5000|5965|3001)" | wc -l)
if [ "$ROGUE_PORTS" -gt 0 ]; then
    echo -e "  ${YELLOW}⚠️  Warning: Found processes on non-standard ports:${NC}"
    lsof -i -P -n | grep LISTEN | grep -E ":(3000|5000|5965|3001)" | awk '{print "     - " $1 " on " $9}'
else
    echo -e "  ${GREEN}✅ No rogue processes detected${NC}"
fi

echo ""
echo "═══════════════════════════════════════════════════"

# Summary
if [ "$PORT_8080" -gt 0 ] && [ "$PORT_5100" -gt 0 ] && [ "$BACKEND_STATUS" = "active" ] && [ "$FRONTEND_STATUS" = "active" ] && [ "$BACKEND_RESPONSE" = "200" ] && [ "$FRONTEND_RESPONSE" = "200" ]; then
    echo -e "${GREEN}✅ SYSTEM STATUS: ALL SYSTEMS OPERATIONAL${NC}"
    echo ""
    echo "🌐 Access your site at: https://houseofprayer.ddns.net"
    echo "🔐 Login: hop / hop@2026ilovejesus"
else
    echo -e "${RED}❌ SYSTEM STATUS: ISSUES DETECTED${NC}"
    echo ""
    echo "Run this to fix:"
    echo "  sudo systemctl restart church-music-backend church-music-frontend"
fi

echo "═══════════════════════════════════════════════════"
