#!/usr/bin/env bash
# ts-check.sh — TypeScript error snapshot for VSR
# Runs tsc --noEmit across all workspaces. Emits output only when errors exist
# (watchdog pattern: silence = green; noise = something to fix).
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"

# Run tsc in each workspace that has a tsconfig
ERROR_OUTPUT=""
WORKSPACES=("apps/vsr" "packages/vsr-graphql-server" "tools/vsr-graphql-stub")

for ws in "${WORKSPACES[@]}"; do
  if [[ ! -f "$ROOT/$ws/tsconfig.json" ]]; then
    continue
  fi
  result=$(cd "$ROOT/$ws" && pnpm exec tsc --noEmit 2>&1 || true)
  if [[ -n "$result" ]]; then
    ERROR_OUTPUT+="### $ws\n\`\`\`\n$result\n\`\`\`\n\n"
  fi
done

if [[ -n "$ERROR_OUTPUT" ]]; then
  echo "## ⚠️ VSR TypeScript Errors — $(date '+%Y-%m-%d %H:%M')"
  echo ""
  echo -e "$ERROR_OUTPUT"
fi
# Empty stdout = no errors = silent (no delivery)
