#!/usr/bin/env bash
# check-generated-content-untouched.sh - Verify committed generated docs match generator output.
#
# Re-runs the 3 page generators (skill, workflow, showcase) into a temp directory
# and diffs against the committed files in docs/. Fails if any committed file
# differs from regenerated output, surfacing hand-edits to generated content at
# PR time rather than at next-deploy time.
#
# Watched paths (all generated by scripts/generate-*.py):
#   docs/skills/          (40 skill pages + 8 phase indices)
#   docs/workflows/       (9 workflow pages + index)
#   docs/showcase/        (3 thread pages + index)
#   docs/reference/commands.md  (single file)
#
# Pairs with Pattern 5C (Bucket A.4): every generated page now declares
# `generated: true` in frontmatter, so this validator's diff implicitly
# enforces flag presence as well as content fidelity.
#
# Exit codes:
#   0 - All generated content matches generator output
#   1 - Hand-edit drift detected (or generator failure)
#
# Usage:
#   ./scripts/check-generated-content-untouched.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
TMP_DIR="$ROOT/.tmp_generated_check"

echo "=== Generated Content Untouched Check ==="
echo ""

# Sanity check: generators present
for gen in generate-skill-pages.py generate-workflow-pages.py generate-showcase.py; do
  if [[ ! -f "$ROOT/scripts/$gen" ]]; then
    echo "FAIL: missing generator at scripts/$gen"
    exit 1
  fi
done

# Clean any prior temp workspace
if [[ -d "$TMP_DIR" ]]; then
  rm -rf "$TMP_DIR"
fi

# Stage the watched docs subset into a temp working tree, run generators, then diff.
# Strategy: copy source-of-truth (skills/, _workflows/, library/) and current docs/
# placeholders to a temp ROOT, run generators in-place against that ROOT, diff.
mkdir -p "$TMP_DIR"

# Mirror only the inputs the generators read; generators write to docs/{skills,workflows,showcase}
# and docs/reference/commands.md. We snapshot original ROOT outputs first, regen, then compare.

SNAPSHOT="$TMP_DIR/snapshot"
REGEN="$TMP_DIR/regen"
mkdir -p "$SNAPSHOT" "$REGEN"

# Snapshot watched outputs as they currently exist
WATCHED_DIRS=(skills workflows showcase)
for d in "${WATCHED_DIRS[@]}"; do
  if [[ -d "$ROOT/docs/$d" ]]; then
    cp -r "$ROOT/docs/$d" "$SNAPSHOT/$d"
  fi
done
if [[ -f "$ROOT/docs/reference/commands.md" ]]; then
  mkdir -p "$SNAPSHOT/reference"
  cp "$ROOT/docs/reference/commands.md" "$SNAPSHOT/reference/commands.md"
fi

# Run generators in place (they write into $ROOT/docs/...)
GEN_OUT=$(mktemp)
trap 'rm -f "$GEN_OUT"' EXIT
if ! python3 "$ROOT/scripts/generate-skill-pages.py" >"$GEN_OUT" 2>&1; then
  echo "FAIL: generate-skill-pages.py exited non-zero"
  cat "$GEN_OUT"
  rm -rf "$TMP_DIR"
  exit 1
fi
if ! python3 "$ROOT/scripts/generate-workflow-pages.py" >"$GEN_OUT" 2>&1; then
  echo "FAIL: generate-workflow-pages.py exited non-zero"
  cat "$GEN_OUT"
  rm -rf "$TMP_DIR"
  exit 1
fi
if ! python3 "$ROOT/scripts/generate-showcase.py" >"$GEN_OUT" 2>&1; then
  echo "FAIL: generate-showcase.py exited non-zero"
  cat "$GEN_OUT"
  rm -rf "$TMP_DIR"
  exit 1
fi

# Stash regenerated outputs for diffing
for d in "${WATCHED_DIRS[@]}"; do
  if [[ -d "$ROOT/docs/$d" ]]; then
    cp -r "$ROOT/docs/$d" "$REGEN/$d"
  fi
done
if [[ -f "$ROOT/docs/reference/commands.md" ]]; then
  mkdir -p "$REGEN/reference"
  cp "$ROOT/docs/reference/commands.md" "$REGEN/reference/commands.md"
fi

# Restore committed content (so this validator does not mutate the working tree)
for d in "${WATCHED_DIRS[@]}"; do
  if [[ -d "$SNAPSHOT/$d" ]]; then
    rm -rf "$ROOT/docs/$d"
    cp -r "$SNAPSHOT/$d" "$ROOT/docs/$d"
  fi
done
if [[ -f "$SNAPSHOT/reference/commands.md" ]]; then
  cp "$SNAPSHOT/reference/commands.md" "$ROOT/docs/reference/commands.md"
fi

# Diff snapshot (committed) vs regen (fresh generator output)
# Use --strip-trailing-cr to ignore CRLF/LF noise on Windows worktrees
FAIL=0
DIFF_OUT=$(mktemp)
trap 'rm -f "$GEN_OUT" "$DIFF_OUT"' EXIT

for d in "${WATCHED_DIRS[@]}"; do
  if [[ -d "$SNAPSHOT/$d" ]] || [[ -d "$REGEN/$d" ]]; then
    if ! diff -r --strip-trailing-cr "$SNAPSHOT/$d" "$REGEN/$d" >>"$DIFF_OUT" 2>&1; then
      FAIL=1
    fi
  fi
done

if [[ -f "$SNAPSHOT/reference/commands.md" ]] || [[ -f "$REGEN/reference/commands.md" ]]; then
  if ! diff --strip-trailing-cr \
      "$SNAPSHOT/reference/commands.md" \
      "$REGEN/reference/commands.md" >>"$DIFF_OUT" 2>&1; then
    FAIL=1
  fi
fi

if [[ "$FAIL" -eq 1 ]]; then
  echo "FAIL: committed generated content differs from generator output."
  echo ""
  echo "Hand-edits to generated files will be overwritten on next regen."
  echo "To fix: re-run the generators and commit the result."
  echo ""
  echo "  python3 scripts/generate-skill-pages.py"
  echo "  python3 scripts/generate-workflow-pages.py"
  echo "  python3 scripts/generate-showcase.py"
  echo ""
  echo "Diff (snapshot=committed, regen=fresh generator output):"
  echo ""
  cat "$DIFF_OUT" | head -200
  if [[ $(wc -l <"$DIFF_OUT") -gt 200 ]]; then
    echo ""
    echo "... (output truncated; see full diff by running the generators locally)"
  fi
  rm -rf "$TMP_DIR"
  exit 1
fi

# Clean
rm -rf "$TMP_DIR"

echo "PASS: all 63 generated pages match generator output."
exit 0
