#!/usr/bin/env bash
# Compare Master (pos3) vs synth hop (.250) for TID 40CC Touch6 path.
set -euo pipefail
TID="${1:-40CC}"
ROLE="${2:-auto}"
EV="${HOME}/EtherGold/dev/data/events.jsonl"
LOG="${HOME}/EtherGold/logs/lab_gameplay_pilot/bridge.log"
echo "host=$(hostname) tid=$TID"
if [[ ! -f "$EV" ]]; then
  echo "missing $EV"
  exit 1
fi
echo "=== counts ==="
echo "tid_lines=$(grep -c "$TID" "$EV" || true)"
echo "eth_to_uart_done_20=$(grep "$TID" "$EV" | grep eth_to_uart_done | grep -c '"msg": "20"' || true)"
echo "eth_tx_29=$(grep "$TID" "$EV" | grep eth_tx | grep -c '"msg": "29"' || true)"
echo "eth_rx_29=$(grep "$TID" "$EV" | grep eth_rx | grep -c '"msg": "29"' || true)"
echo "any_29=$(grep "$TID" "$EV" | grep -c '"msg": "29"' || true)"
echo "any_21=$(grep "$TID" "$EV" | grep -c '"msg": "21"' || true)"
echo "stale=$(grep "$TID" "$EV" | grep -c trunk_round_stale_abandon || true)"
echo "=== last 15 tid lines (compact) ==="
grep "$TID" "$EV" | tail -15 | python3 -c '
import sys, json
for line in sys.stdin:
    try:
        e = json.loads(line)
    except Exception:
        continue
    print(
        e.get("utc","")[-12:],
        e.get("ev"),
        "msg="+str(e.get("msg") or e.get("sent_msg") or ""),
        "port="+str(e.get("eth_port") or ""),
        "seq="+str(e.get("play_sequence") or ""),
        "x9="+str(e.get("x9_injected")),
    )
'
if [[ -f "$LOG" ]]; then
  echo "=== pilot log stale/wager tail ==="
  grep -E "lab_gameplay_synth_wager|trunk_round_stale_abandon|$TID" "$LOG" | tail -12
fi
