#!/bin/bash

echo "Checking EMS Platform Health..."
echo "--------------------------------"

# Check Containers
echo "[1] Checking Container Status:"
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep ems_
echo ""

# Check Backend Health
echo "[2] Checking Backend Health (Direct):"
if docker exec ems_platform_app-server_1 wget -qO- http://localhost:8080/ping > /dev/null 2>&1; then
    echo "✅ Backend is UP"
else
    echo "❌ Backend is DOWN or Unreachable"
fi
echo ""

# Check Nginx Proxy
echo "[3] Checking Frontend/Proxy:"
if curl -s -I http://localhost/api/v1/info | grep "200 OK" > /dev/null; then
    echo "✅ Nginx Proxy to Backend is WORKING"
else
    echo "❌ Nginx Proxy check FAILED"
    echo "Debug Info:"
    curl -I http://localhost/api/v1/info
fi
echo ""

# Check Database Connectivity (via Logs)
echo "[4] Recent Database Errors in Logs:"
docker logs --tail 50 ems_platform_app-server_1 | grep -i "error\|fail\|refused"
echo ""

echo "Done."

