#!/bin/bash
# Health Check Script for House of Prayer Music Database
# Verifies all services are running correctly

echo "================================"
echo "HOUSE OF PRAYER - HEALTH CHECK"
echo "================================"
echo ""

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

# Counters
PASSED=0
FAILED=0
WARNINGS=0

# Function to check service
check_service() {
    local name=$1
    local check_cmd=$2
    local test_cmd=$3
    
    echo -n "Checking $name... "
    
    if eval "$check_cmd" > /dev/null 2>&1; then
        if [ -n "$test_cmd" ]; then
            if eval "$test_cmd" > /dev/null 2>&1; then
                echo -e "${GREEN}✓ PASS${NC}"
                ((PASSED++))
            else
                echo -e "${YELLOW}⚠ RUNNING but test failed${NC}"
                ((WARNINGS++))
            fi
        else
            echo -e "${GREEN}✓ PASS${NC}"
            ((PASSED++))
        fi
    else
        echo -e "${RED}✗ FAIL${NC}"
        ((FAILED++))
    fi
}

# Check Frontend (Vite Dev Server)
check_service "Frontend (port 5100)" \
    "lsof -i:5100 -sTCP:LISTEN" \
    "curl -s -o /dev/null -w '%{http_code}' http://localhost:5100/ | grep -q 200"

# Check Backend (Node API)
check_service "Backend (port 8080)" \
    "lsof -i:8080 -sTCP:LISTEN" \
    "curl -s -o /dev/null -w '%{http_code}' http://localhost:8080/api/songs | grep -q 200"

# Check Nginx
check_service "Nginx (port 443)" \
    "sudo lsof -i:443 -sTCP:LISTEN" \
    ""

# Check PostgreSQL
check_service "PostgreSQL" \
    "sudo systemctl is-active postgresql" \
    ""

# Check Vite Host Configuration
echo -n "Checking Vite host config... "
if curl -s -H "Host: houseofprayer.ddns.net" -o /dev/null -w "%{http_code}" http://localhost:5100/ | grep -q 200; then
    echo -e "${GREEN}✓ PASS (domain accepted)${NC}"
    ((PASSED++))
else
    echo -e "${RED}✗ FAIL (domain blocked)${NC}"
    echo "  → Check frontend/vite.config.js allowedHosts"
    ((FAILED++))
fi

# Check DNS Resolution
echo -n "Checking DNS resolution... "
if nslookup houseofprayer.ddns.net | grep -q "170.254.17.146"; then
    echo -e "${GREEN}✓ PASS${NC}"
    ((PASSED++))
else
    echo -e "${YELLOW}⚠ WARNING (DNS may not resolve correctly)${NC}"
    ((WARNINGS++))
fi

# Check External HTTPS Access
echo -n "Checking public HTTPS access... "
if curl -s -o /dev/null -w "%{http_code}" https://houseofprayer.ddns.net | grep -q 200; then
    echo -e "${GREEN}✓ PASS${NC}"
    ((PASSED++))
else
    echo -e "${RED}✗ FAIL${NC}"
    ((FAILED++))
fi

# Summary
echo ""
echo "================================"
echo "SUMMARY"
echo "================================"
echo -e "${GREEN}Passed:${NC}   $PASSED"
echo -e "${YELLOW}Warnings:${NC} $WARNINGS"
echo -e "${RED}Failed:${NC}   $FAILED"
echo ""

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}✓ All critical services operational!${NC}"
    echo ""
    echo "Site accessible at: https://houseofprayer.ddns.net"
    exit 0
else
    echo -e "${RED}✗ Some services are down. Check logs for details.${NC}"
    echo ""
    echo "Quick fixes:"
    echo "  - Frontend: cd frontend && npm run dev &"
    echo "  - Backend: cd backend && node server.js &"
    echo "  - Nginx: sudo systemctl restart nginx"
    exit 1
fi
