#!/usr/bin/env bash
# caddie-watch.sh — has any caddie standard changed since we last looked?
#
# Ticket is a caddie drop-in and caddie is a moving target. This script hashes
# the files that DEFINE what a caddie app is (the planning docs, the runbook,
# the caddie-ui pack, the interaction embryo, the BW-app standard docs) and
# compares them with the snapshot taken at the last review.
#
#   tools/caddie-watch.sh            # report: changed / new / gone since snapshot
#   tools/caddie-watch.sh --update   # accept the current state as the new snapshot
#   tools/caddie-watch.sh --list     # print the watched paths
#
# Run it at the start of every session. When it reports changes, read the diff
# (git is not available on these workspaces — read the file and its handoff),
# update §1 of .logs/planning/02-caddie-feedback.md, then --update.
set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SNAP="$HERE/.logs/planning/caddie-snapshot.sha256"

# Directories are expanded to their files; missing paths are reported, not fatal.
WATCH=(
  /srv/apps/caddie/.logs/planning
  /srv/apps/caddie/brief.md
  /srv/apps/caddie/agents.md
  /srv/apps/caddie/CHANGELOG.md
  /srv/apps/caddie/main/app/services/interaction.py
  /srv/apps/caddie/main/app/services/attachments.py
  /srv/apps/easel/.logs/planning/caddie-ui-standard-proposal.md
  /srv/apps/easel/main/frontend/src/caddie-ui
  /srv/apps/punchlist/.logs/planning/01-plan.md
  /srv/system/id-auth/app-auth/QUICKSTART-BW-APP.md
  /srv/system/id-auth/app-auth/PACKS.md
  /srv/system/id-auth/app-auth/VIEW-AS.md
  /srv/system/id-auth/app-auth/react-admin/UI-STANDARD.md
  /srv/.logs/planning/bw-app-standard-2026.md
  /srv/apps/with/docs/SATELLITE-CONTRACT.md
)

expand() {
  local p
  for p in "${WATCH[@]}"; do
    if [[ -d "$p" ]]; then
      find "$p" -type f \( -name '*.md' -o -name '*.py' -o -name '*.ts' -o -name '*.tsx' -o -name '*.css' \) | sort
    elif [[ -f "$p" ]]; then
      echo "$p"
    else
      echo "MISSING: $p" >&2
    fi
  done
}

current() { expand | xargs -r sha256sum; }

case "${1:-}" in
  --list) expand ;;
  --update)
    current > "$SNAP"
    chmod 664 "$SNAP" 2>/dev/null || true
    echo "snapshot written: $SNAP ($(wc -l < "$SNAP") files, $(date +%F))"
    ;;
  "")
    if [[ ! -f "$SNAP" ]]; then
      echo "no snapshot yet — run: $0 --update"; exit 1
    fi
    cur="$(mktemp)"; current > "$cur"
    changed=0
    # changed or new
    while read -r hash path; do
      old="$(awk -v p="$path" '$2==p{print $1}' "$SNAP")"
      if [[ -z "$old" ]]; then echo "NEW      $path"; changed=1
      elif [[ "$old" != "$hash" ]]; then echo "CHANGED  $path  ($(stat -c %y "$path" | cut -d. -f1))"; changed=1
      fi
    done < "$cur"
    # gone
    while read -r hash path; do
      awk -v p="$path" '$2==p{f=1} END{exit !f}' "$cur" || { echo "GONE     $path"; changed=1; }
    done < "$SNAP"
    rm -f "$cur"
    if [[ $changed -eq 0 ]]; then
      echo "caddie standards unchanged since snapshot ($(stat -c %y "$SNAP" | cut -d. -f1))"
    else
      echo; echo "→ read the changes, update .logs/planning/02-caddie-feedback.md §1, then: $0 --update"
    fi
    ;;
  *) echo "usage: $0 [--update|--list]"; exit 2 ;;
esac
