#!/usr/bin/env bash
# Tiny status bar indicators for trail entries and session attention.
# Called by tmux status-right via #(~/.config/mux/status-indicators.sh)

SESSION=$(tmux display-message -p '#{session_name}' 2>/dev/null)
TRAIL_DIR="$HOME/.local/share/mux/trails"
STATUS_DIR="$HOME/.local/share/mux/status"

OUT=""

# Trail count for current session
TRAIL_FILE="$TRAIL_DIR/$SESSION.jsonl"
if [ -f "$TRAIL_FILE" ]; then
    COUNT=$(wc -l < "$TRAIL_FILE" | tr -d ' ')
    if [ "$COUNT" -gt 0 ]; then
        OUT="◆${COUNT} "
    fi
fi

# Sessions needing attention (waiting or error)
WAITING=0
ERRORS=0
if [ -d "$STATUS_DIR" ]; then
    for f in "$STATUS_DIR"/*.json; do
        [ -f "$f" ] || continue
        STATE=$(MUX_STATUS_FILE="$f" python3 -c "
import json, os
print(json.load(open(os.environ['MUX_STATUS_FILE'])).get('state',''))
" 2>/dev/null)
        case "$STATE" in
            waiting) WAITING=$((WAITING + 1)) ;;
            error)   ERRORS=$((ERRORS + 1)) ;;
        esac
    done
fi

if [ "$ERRORS" -gt 0 ]; then
    OUT="${OUT}#[fg=red]✗${ERRORS}#[default] "
fi
if [ "$WAITING" -gt 0 ]; then
    OUT="${OUT}#[fg=yellow]○${WAITING}#[default] "
fi

printf "%s" "$OUT"
