#!/bin/bash
# Verification script for WebSocket HTTPS fix
# Run this to confirm the fix is working correctly

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║     WebSocket HTTPS Fix - Verification Script                 ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

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

# Check 1: Frontend service status
echo "1. Checking frontend service status..."
if systemctl is-active --quiet church-music-frontend.service; then
    echo -e "${GREEN}✓ Frontend service is running${NC}"
else
    echo -e "${RED}✗ Frontend service is NOT running${NC}"
    exit 1
fi

# Check 2: Build directory exists
echo ""
echo "2. Checking build directory..."
if [ -d "/media/pts/Website/Church_HOP_MusicData/frontend/build" ]; then
    echo -e "${GREEN}✓ Build directory exists${NC}"
else
    echo -e "${RED}✗ Build directory not found${NC}"
    exit 1
fi

# Check 3: No webpack-dev-server in build
echo ""
echo "3. Checking for webpack-dev-server in build..."
WDS_COUNT=$(grep -r "webpack-dev-server" /media/pts/Website/Church_HOP_MusicData/frontend/build/ 2>/dev/null | wc -l)
if [ "$WDS_COUNT" -eq 0 ]; then
    echo -e "${GREEN}✓ No webpack-dev-server found in build (clean)${NC}"
else
    echo -e "${RED}✗ Found $WDS_COUNT webpack-dev-server references${NC}"
    exit 1
fi

# Check 4: .env.production exists
echo ""
echo "4. Checking .env.production file..."
if [ -f "/media/pts/Website/Church_HOP_MusicData/frontend/.env.production" ]; then
    echo -e "${GREEN}✓ .env.production file exists${NC}"
else
    echo -e "${YELLOW}⚠ .env.production file not found (optional)${NC}"
fi

# Check 5: WDS_SOCKET_PROTOCOL in .env
echo ""
echo "5. Checking .env configuration..."
if grep -q "WDS_SOCKET_PROTOCOL=wss" /media/pts/Website/Church_HOP_MusicData/frontend/.env 2>/dev/null; then
    echo -e "${GREEN}✓ WDS_SOCKET_PROTOCOL=wss is set${NC}"
else
    echo -e "${YELLOW}⚠ WDS_SOCKET_PROTOCOL not set (needed for dev mode only)${NC}"
fi

# Check 6: HTTPS site is accessible
echo ""
echo "6. Checking HTTPS accessibility..."
if curl -s -o /dev/null -w "%{http_code}" https://houseofprayer.ddns.net | grep -q "200"; then
    echo -e "${GREEN}✓ HTTPS site is accessible (200 OK)${NC}"
else
    echo -e "${RED}✗ HTTPS site returned non-200 status${NC}"
fi

# Check 7: No WebSocket errors in recent logs
echo ""
echo "7. Checking service logs for WebSocket errors..."
if journalctl -u church-music-frontend.service --since "10 minutes ago" 2>/dev/null | grep -qi "websocket\|wds_socket"; then
    echo -e "${YELLOW}⚠ Found WebSocket references in logs (check manually)${NC}"
else
    echo -e "${GREEN}✓ No WebSocket errors in recent logs${NC}"
fi

# Check 8: Bundle size verification
echo ""
echo "8. Checking production bundle..."
MAIN_JS=$(find /media/pts/Website/Church_HOP_MusicData/frontend/build/static/js/ -name "main.*.js" 2>/dev/null | head -1)
if [ -f "$MAIN_JS" ]; then
    SIZE=$(du -h "$MAIN_JS" | cut -f1)
    echo -e "${GREEN}✓ Production bundle found: $SIZE${NC}"
else
    echo -e "${RED}✗ Production bundle not found${NC}"
    exit 1
fi

# Final summary
echo ""
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║                    VERIFICATION COMPLETE                       ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
echo -e "${GREEN}All checks passed! The WebSocket HTTPS fix is working correctly.${NC}"
echo ""
echo "Next steps:"
echo "  1. Clear browser cache: Ctrl+Shift+Delete"
echo "  2. Force reload: Ctrl+Shift+R (or Cmd+Shift+R on Mac)"
echo "  3. Open console: F12"
echo "  4. Navigate to: https://houseofprayer.ddns.net"
echo "  5. Verify no WebSocket errors appear"
echo ""
echo "Documentation: WEBSOCKET_HTTPS_FIX.md"
