#!/usr/bin/env bash
# Objective completion check for the tpengdesign.com redesign slate.
# exit 0 = genuinely done. exit 1 = not done, reason on stdout.
# Nothing here trusts what the session claimed; it re-fetches and re-counts.
set -uo pipefail

D="${SLATE_DIR:-$HOME/projects/tpengdesign-slate}"
PORT="${SLATE_PORT:-8078}"
WANT="${SLATE_COUNT:-20}"
BASE="http://127.0.0.1:${PORT}"

fail() { echo "$*"; exit 1; }

[ -d "$D/concepts" ] || fail "no concepts/ dir at $D"

n=$(find "$D/concepts" -maxdepth 1 -mindepth 1 -type d | wc -l)
[ "$n" -eq "$WANT" ] || fail "$n/$WANT concept directories exist"

curl -sf -m 5 "$BASE/" -o /dev/null || fail "index not serving on $BASE"

blank=()
for c in "$D"/concepts/*/; do
  slug=$(basename "$c")
  [ -f "$c/index.html" ] || { blank+=("$slug:no-index.html"); continue; }
  bytes=$(curl -sf -m 5 "$BASE/concepts/$slug/" | wc -c)
  [ "$bytes" -gt 2000 ] || blank+=("$slug:${bytes}b")
done
[ ${#blank[@]} -eq 0 ] || fail "${#blank[@]} concept(s) blank or unreachable: ${blank[*]}"

for f in NOTES.md CONTENT-GAPS.md SLATE.md registry.md; do
  [ -s "$D/$f" ] || fail "missing or empty $f"
done

# registry must record one distinct organizing principle per concept
principles=$(grep -cE '^[-*] ' "$D/registry.md" 2>/dev/null || echo 0)
[ "$principles" -ge "$WANT" ] || fail "registry.md lists $principles principles, need $WANT"

echo "slate verified: $WANT/$WANT concepts serving, all artifacts present"
exit 0
