#!/bin/bash
SLACK_API="https://blackroad-slack.amundsonalexa.workers.dev"
NODE_NAME="alexandria"
P='\033[38;5;205m'; G='\033[38;5;82m'; R='\033[0m'
case "${1:-help}" in
  say|post) curl -s -X POST "$SLACK_API/post" -H 'Content-Type: application/json' -d "{\"text\":\"*${NODE_NAME}:* ${*:2}\"}" >/dev/null; echo -e "${G}✓${R} Posted";;
  alert) curl -s -X POST "$SLACK_API/alert" -H 'Content-Type: application/json' -d "{\"text\":\"*${NODE_NAME}:* ${*:2}\"}" >/dev/null; echo -e "${G}✓${R} Alert sent";;
  ask) curl -s -X POST "$SLACK_API/ask" -H 'Content-Type: application/json' -d "{\"agent\":\"${2:-road}\",\"message\":\"${*:3}\",\"slack\":true}" | python3 -c "import sys,json;print(json.load(sys.stdin).get('reply','?'))" 2>/dev/null;;
  collab) curl -s -X POST "$SLACK_API/collab" -H 'Content-Type: application/json' -d "{\"type\":\"announce\",\"session_id\":\"${NODE_NAME}-$(date +%s)\",\"message\":\"${*:2}\"}" >/dev/null; echo -e "${G}✓${R} Sent";;
  til) curl -s -X POST "$SLACK_API/memory" -H 'Content-Type: application/json' -H 'User-Agent: BlackRoad-Pi/1.0' -d "{\"type\":\"til\",\"category\":\"${2:-tip}\",\"learning\":\"${*:3}\",\"broadcaster\":\"$NODE_NAME\"}" >/dev/null; echo -e "${G}✓${R} TIL";;
  log) curl -s -X POST "$SLACK_API/memory" -H 'Content-Type: application/json' -H 'User-Agent: BlackRoad-Pi/1.0' -d "{\"type\":\"journal\",\"action\":\"${2:-log}\",\"entity\":\"${3:-?}\",\"details\":\"${*:4}\",\"source\":\"$NODE_NAME\"}" >/dev/null; echo -e "${G}✓${R} Logged";;
  health) curl -s "$SLACK_API/health" | python3 -m json.tool 2>/dev/null;;
  agents) curl -s "$SLACK_API/agents" | python3 -c "import json,sys;[print(f'{a[\"emoji\"]} {a[\"name\"]:14} — {a[\"role\"]}') for a in json.load(sys.stdin)['agents']];print()" 2>/dev/null;;
  status) curl -s "$SLACK_API/memory/stats" | python3 -m json.tool 2>/dev/null;;
  *) echo -e "${P}br-slack${R} — say|alert|ask|collab|til|log|health|agents|status";;
esac
