#!/bin/bash
# Master-boss polling script for AI Council
# Polls message queue every 10 seconds and reports pending messages

TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIwNzlmNDRhYS05YTg3LTQzZTctOGUyNS0yNDhjYWMxMDQ0ODEiLCJyb2xlIjoiYm9zcyIsImV4cCI6MTc2NjM3ODA2MH0.30PQk9WC_M82_ZC4YG2Mbcm24inVqMIn-PdkNACUyOw"
API_BASE="https://ai.glocalis.com/api/v2"

echo "Starting master-boss polling..."
echo "Press Ctrl+C to stop"

while true; do
    # Poll for pending messages
    PENDING=$(curl -s "$API_BASE/queue/pending" -H "Authorization: Bearer $TOKEN")

    if [ "$PENDING" != "[]" ] && [ -n "$PENDING" ]; then
        echo ""
        echo "=== $(date '+%Y-%m-%d %H:%M:%S') ==="
        echo "Pending messages found:"
        echo "$PENDING" | jq -r '.[] | "Queue ID: \(.id) | User: \(.username) | Message: \(.content)"' 2>/dev/null || echo "$PENDING"
        echo ""
        echo ">>> Action required: Process messages above <<<"
    fi

    sleep 10
done
