#!/bin/bash

API_URL=$1

echo "Starting monitor script..."
echo "API URL: $API_URL"
echo "------------------------------------------------"

while true; do
    HTTP_STATUS=$(curl -o /dev/null -s -w "%{http_code}" -m 5 "$API_URL")
    
    if [ "$HTTP_STATUS" -eq 200 ]; then
        response=$(curl -s -m 5 "$API_URL")
        
        status=$(echo "$response" | jq -r '.status' 2>/dev/null)
        if [ -n "$status" ] && [ "$status" != "null" ]; then
            echo "Status: $status | Response Time: ${response_time}ms"
        else
            echo "HTTP 200 OK, but invalid JSON format received."
        fi
    elif [ "$HTTP_STATUS" -eq 000 ]; then
        echo "Application is Offline or unreachable (Timeout)."
    else
        echo "Application is unhealthy. HTTP Status: $HTTP_STATUS"
    fi
    
    sleep 30
done