#!/usr/bin/env bash
# br-status - Quick terminal status view
# Production-grade fleet + services status
set -eo pipefail

# Source centralized config
NODES_CONFIG="$HOME/.blackroad/config/nodes.sh"
if [[ -f "$NODES_CONFIG" ]]; then
  source "$NODES_CONFIG"
else
  PINK='\033[38;5;205m'; AMBER='\033[38;5;214m'; GREEN='\033[38;5;82m'
  BLUE='\033[38;5;69m'; RED='\033[38;5;196m'; RESET='\033[0m'
fi

printf '%b╭─────────────────────────────────────────────────────────────────────────────╮%b\n' "$PINK" "$RESET"
printf '%b│%b              %b⬡%b %bBLACKROAD INFRASTRUCTURE STATUS%b                           %b│%b\n' "$PINK" "$RESET" "$AMBER" "$RESET" "$PINK" "$RESET" "$PINK" "$RESET"
printf '%b╰─────────────────────────────────────────────────────────────────────────────╯%b\n\n' "$PINK" "$RESET"

# Local services
printf '%bLOCAL SERVICES:%b\n' "$BLUE" "$RESET"
services=("ollama:11434" "docker:2375" "postgres:5432" "redis:6379")
for svc in "${services[@]}"; do
  name="${svc%:*}"
  port="${svc#*:}"
  if nc -z localhost "$port" 2>/dev/null; then
    printf '  %b●%b %s (:%s)\n' "$GREEN" "$RESET" "$name" "$port"
  else
    printf '  %b○%b %s (:%s)\n' "$RED" "$RESET" "$name" "$port"
  fi
done
echo ""

# Fleet nodes
printf '%bFLEET NODES:%b\n' "$BLUE" "$RESET"
for node in "${PI_NODES[@]}"; do
  ip="${NODE_IP[$node]:-}"
  user="${NODE_USER[$node]:-}"
  [[ -z "$ip" ]] && continue

  if ping -c 1 -W 1 "$ip" &>/dev/null; then
    load=$(ssh -o ConnectTimeout=2 -o BatchMode=yes -o LogLevel=ERROR "${user}@${ip}" \
      "uptime | awk -F'load average: ' '{print \$2}' | cut -d',' -f1" 2>/dev/null || echo "?")
    temp=$(ssh -o ConnectTimeout=2 -o BatchMode=yes -o LogLevel=ERROR "${user}@${ip}" \
      "vcgencmd measure_temp 2>/dev/null | grep -oP '[0-9.]+'" 2>/dev/null || echo "?")
    printf '  %b●%b %-10s (%s) load: %-6s temp: %s°C\n' "$GREEN" "$RESET" "$node" "$ip" "${load:-?}" "${temp:-?}"
  else
    printf '  %b○%b %-10s (%s) offline\n' "$RED" "$RESET" "$node" "$ip"
  fi
done
echo ""

# Quick stats
printf '%bQUICK STATS:%b\n' "$BLUE" "$RESET"

# Count local repos
repo_count=$(find "$HOME" -maxdepth 1 -name '.git' -o -name 'blackroad-*' -type d 2>/dev/null | wc -l | tr -d ' ')
printf '  Local repos: %s\n' "$repo_count"

# Docker containers (local)
if command -v docker &>/dev/null; then
  containers=$(docker ps -q 2>/dev/null | wc -l | tr -d ' ')
  printf '  Docker containers: %s\n' "$containers"
fi

# Memory events
if [[ -f "$HOME/.blackroad/memory/journals/master-journal.jsonl" ]]; then
  events=$(wc -l < "$HOME/.blackroad/memory/journals/master-journal.jsonl" 2>/dev/null || echo "0")
  printf '  Memory events: %s\n' "$(echo "$events" | tr -d ' ')"
fi

# Disk usage
disk=$(df -h / | awk 'NR==2{print $5" used ("$4" free)"}')
printf '  Disk: %s\n' "$disk"

echo ""
printf '%b─────────────────────────────────────────────────────────────────────────────%b\n' "$PINK" "$RESET"
printf '  Full: %bbr-help%b | Dashboard: %bbr-dashboard%b | Fleet: %bbrctl status%b\n' "$AMBER" "$RESET" "$AMBER" "$RESET" "$AMBER" "$RESET"
