#!/bin/bash
set -euo pipefail

# NAS 내부 헬스체크용 (localhost 사용)

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

echo "======================================"
echo " Starting Health Check..."
echo "======================================"

check_url() {
    local name="$1"
    local url="$2"

    # HTTP 상태 코드만 가져옴 (타임아웃 5초)
    status=$(curl -o /dev/null -s -w "%{http_code}" --max-time 5 "$url" || echo "FAIL")

    if [[ "$status" == "200" ]]; then
        echo -e "[${GREEN}OK${NC}] $name ($url) -> $status"
    else
        echo -e "[${RED}XX${NC}] $name ($url) -> $status"
    fi
}

echo ""
echo "--- 1. Backend (Lotto) ---"
check_url "Backend Health" "http://localhost:18000/health"
check_url "Lotto Latest" "http://localhost:18000/api/lotto/latest"
check_url "Stats API" "http://localhost:18000/api/lotto/stats"

echo ""
echo "--- 2. Stock Lab ---"
check_url "Stock Health" "http://localhost:18500/health"
check_url "Stock News" "http://localhost:18500/api/stock/news"
check_url "Stock Indices" "http://localhost:18500/api/stock/indices"

echo ""
echo "--- 3. Music Lab ---"
check_url "Music Health" "http://localhost:18600/health"
check_url "Music Providers" "http://localhost:18600/api/music/providers"

echo ""
echo "--- 4. Insta Lab ---"
check_url "Insta Health" "http://localhost:18700/health"
check_url "Insta Status" "http://localhost:18700/api/insta/status"

echo ""
echo "--- 5. Realestate Lab ---"
check_url "Realestate Health" "http://localhost:18800/health"

echo ""
echo "--- 6. Agent Office ---"
check_url "Agent Office Health" "http://localhost:18900/health"
check_url "Agent Office Agents" "http://localhost:18900/api/agent-office/agents"

echo ""
echo "--- 7. Tarot Lab ---"
check_url "Tarot Health" "http://localhost:18250/health"
check_url "Tarot Readings" "http://localhost:18250/api/tarot/readings"

echo ""
echo "--- 8. Saju Lab ---"
check_url "Saju Health" "http://localhost:18300/health"
check_url "Saju Readings" "http://localhost:18300/api/saju/readings"

echo ""
echo "--- 9. Travel Proxy ---"
check_url "Travel Regions" "http://localhost:19000/api/travel/regions"

echo ""
echo "--- 10. Frontend (Nginx) ---"
check_url "Frontend Home" "http://localhost:8080"
check_url "Nginx->Backend Proxy" "http://localhost:8080/api/lotto/latest"
check_url "Nginx->Tarot Proxy" "http://localhost:8080/api/tarot/readings"
check_url "Nginx->Saju Proxy" "http://localhost:8080/api/saju/readings"

echo ""
echo "======================================"
echo " Health Check Completed."
echo "======================================"
