#!/bin/bash
# comeback.sh — "what did I miss?" sit-rep generator.
#
# Per HQ-Vault/_System/CLAUDE.md "The comeback protocol", reads the last 14
# days of dailies, every project _Log.md, and recent _AgentLog/ entries.
# Dumps them grouped + a per-agent activity tally so Rocky can scan in
# 30 seconds instead of opening 8 files in Obsidian.
#
# Usage:
#   bash scripts/comeback.sh             # last 14 days
#   bash scripts/comeback.sh 7           # last 7 days
#   bash scripts/comeback.sh 30 > sitrep.md   # 30 days, write to file

set -u
DAYS="${1:-14}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
VAULT="$ROOT/HQ-Vault"

if [ ! -d "$VAULT" ]; then
  echo "ERROR: HQ-Vault not found at $VAULT" >&2
  exit 1
fi

cd "$VAULT"

echo "# Comeback sit-rep — $(date -u +%Y-%m-%d) (last $DAYS days)"
echo
echo "_Generated by \`scripts/comeback.sh\`. Per AI Team Protocol section 'The comeback protocol'._"
echo

# --- Activity tally per agent ---
echo "## Activity tally"
echo
{
  # Daily-note headings: ## HH:MM — agent — title (require both em-dashes)
  find 01_Daily -name "*.md" -mtime -"$DAYS" -print0 2>/dev/null \
    | xargs -0 grep -hE "^## [0-9]{2}:[0-9]{2} — .+ — " 2>/dev/null \
    | sed -E "s/^## [0-9]{2}:[0-9]{2} — ([^—]+) — .*/\1/" \
    | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
  # _Log.md one-liners: - YYYY-MM-DD HH:MM — agent — summary
  find . -name "_Log.md" -path "*/_Brain/_Log.md" -mtime -"$DAYS" -print0 2>/dev/null \
    | xargs -0 grep -hE "^- [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} — .+ — " 2>/dev/null \
    | sed -E "s/^- [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} — ([^—]+) — .*/\1/" \
    | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
} | sort | uniq -c | sort -rn | awk '{count=$1; $1=""; sub(/^[ ]+/,""); printf "- %-25s %d entries\n", $0, count}'
echo

# --- Last N days of dailies ---
echo "## Daily notes (last $DAYS days, most recent first)"
echo
find 01_Daily -name "*.md" -mtime -"$DAYS" 2>/dev/null \
  | sort -r \
  | while read f; do
    echo "---"
    echo
    echo "### \`$f\` (mtime: $(date -r "$f" '+%Y-%m-%d %H:%M'))"
    echo
    cat "$f"
    echo
  done

# --- All project _Log.md files (full, append-only one-liners) ---
echo "## Project logs (full)"
echo
find . -name "_Log.md" -path "*/_Brain/_Log.md" 2>/dev/null \
  | sort \
  | while read f; do
    echo "---"
    echo
    echo "### \`$f\`"
    echo
    cat "$f"
    echo
  done

# --- Agent logs touched in last 7 days ---
echo "## Recent agent logs (last 7 days)"
echo
find . -path "*/_AgentLog/*.md" -mtime -7 -not -name ".gitkeep" 2>/dev/null \
  | sort -r \
  | while read f; do
    echo "---"
    echo
    echo "### \`$f\` (mtime: $(date -r "$f" '+%Y-%m-%d %H:%M'))"
    echo
    head -50 "$f"
    lines=$(wc -l < "$f")
    if [ "$lines" -gt 50 ]; then
      echo
      echo "_(... $((lines - 50)) more lines — open the file for the rest)_"
    fi
    echo
  done

echo "---"
echo
echo "_End of comeback sit-rep. Apply your judgment to bucket into Stalled / Hot / Promised-not-done / Autonomous / Pick-up-first per AI Team Protocol._"
