#!/bin/bash
# Health Check Script for Audit Log Service

SERVICE_NAME="audit-log-service"
SERVICE_PORT=8098
MAX_RETRIES=30
RETRY_INTERVAL=2

check_health() {
    response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:${SERVICE_PORT}/actuator/health 2>/dev/null)
    if [ "$response" = "200" ]; then
        return 0
    else
        return 1
    fi
}

echo "Starting health check for ${SERVICE_NAME}..."

for i in $(seq 1 $MAX_RETRIES); do
    if check_health; then
        echo "✅ ${SERVICE_NAME} is healthy (attempt $i)"
        exit 0
    else
        echo "⏳ ${SERVICE_NAME} is not ready (attempt $i/$MAX_RETRIES)"
        sleep $RETRY_INTERVAL
    fi
done

echo "❌ ${SERVICE_NAME} health check failed after $MAX_RETRIES attempts"
exit 1
