#!/bin/bash
# =============================================================================
# Presence Service - Health Check Script
# =============================================================================
# Checks all dependent services and reports their status.
# =============================================================================

echo "🏥 Starting health check..."
echo ""

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# Counters
PASS=0
FAIL=0
FAILED_SERVICES=()

# Check function
check_service() {
    local service_name=$1
    local check_command=$2
    local fix_command=$3

    echo -n "Checking $service_name ... "

    if eval "$check_command" > /dev/null 2>&1; then
        echo -e "${GREEN}✓ OK${NC}"
        PASS=$((PASS + 1))
    else
        echo -e "${RED}✗ FAIL${NC}"
        FAIL=$((FAIL + 1))
        FAILED_SERVICES+=("$service_name:$fix_command")
    fi
}

# Check PostgreSQL
echo -e "${YELLOW}=== Database Service ===${NC}"
if command -v pg_isready &> /dev/null; then
    check_service "PostgreSQL" "pg_isready -h localhost -p 5432" "docker compose -f docker-compose.dev.yml up -d postgres"
else
    check_service "PostgreSQL" "nc -zv localhost 5432" "docker compose -f docker-compose.dev.yml up -d postgres"
fi

# Check Redis
echo -e "${YELLOW}=== Cache Service ===${NC}"
if command -v redis-cli &> /dev/null; then
    check_service "Redis" "redis-cli -h localhost -p 6379 ping" "docker compose -f docker-compose.dev.yml up -d redis"
elif command -v docker &> /dev/null; then
    check_service "Redis" "docker exec presence-redis-dev redis-cli ping" "docker compose -f docker-compose.dev.yml up -d redis"
else
    check_service "Redis" "nc -zv localhost 6379" "docker compose -f docker-compose.dev.yml up -d redis"
fi

# Check Kafka
echo -e "${YELLOW}=== Message Queue Service ===${NC}"
check_service "Kafka" "nc -zv localhost 9092" "docker compose -f docker-compose.dev.yml up -d kafka"

# Check Presence Service
echo -e "${YELLOW}=== Application Service ===${NC}"
check_service "Presence Service" "curl -sf http://localhost:3011/api/v1/health" "npm run start:dev"

# Summary
echo ""
echo -e "${YELLOW}======================================${NC}"
echo -e "${YELLOW}Health Check Complete!${NC}"
echo -e "${GREEN}Passed: $PASS${NC}"
echo -e "${RED}Failed: $FAIL${NC}"
echo -e "${YELLOW}======================================${NC}"

if [ $FAIL -eq 0 ]; then
    echo -e "${GREEN}✓ All services are healthy!${NC}"
    echo ""
    echo -e "${BLUE}You can now run tests:${NC}"
    echo "  npm test"
    echo "  npm run test:e2e"
    exit 0
else
    echo -e "${RED}✗ Some services are unhealthy!${NC}"
    echo ""
    echo -e "${BLUE}Fix suggestions:${NC}"
    for service_info in "${FAILED_SERVICES[@]}"; do
        service_name="${service_info%%:*}"
        fix_command="${service_info#*:}"
        echo -e "${YELLOW}  • $service_name:${NC} $fix_command"
    done
    echo ""
    echo -e "${BLUE}Or run the start-all script:${NC}"
    echo "  ./scripts/start-all.sh"
    exit 1
fi
