#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2024-2026 SuperNovae Studio <contact@supernovae.studio>
#
# refresh-status.sh — single source of truth for status claims.
#
# Reads the engine state from the canonical sources (git, Cargo.toml,
# cargo test, cargo clippy) and prints a markdown block that every
# status doc (MEMORY.md, STATE.md, CLAUDE.md, ROADMAP.md current-state
# section, etc.) MUST quote verbatim.
#
# Usage:
#   ./scripts/refresh-status.sh           # print canonical block
#   ./scripts/refresh-status.sh --quick   # skip cargo test (fast preview)
#
# Companion: scripts/hygiene/check-status-claims-sync.sh (Phase B vector
# 26) greps the status docs and fails if their claims don't match this
# script's output.

set -euo pipefail

ENGINE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ENGINE_ROOT"

QUICK="${1:-}"

# ── git ────────────────────────────────────────────────────────────
HEAD_SHA_FULL="$(git rev-parse HEAD)"
HEAD_SHA_SHORT="$(git rev-parse --short=9 HEAD)"
BRANCH="$(git branch --show-current)"

# ── workspace ──────────────────────────────────────────────────────
WORKSPACE_VERSION="$(grep -m1 '^version' Cargo.toml | sed -E 's/^version[ \t]+=[ \t]+"([^"]+)".*/\1/')"

# Workspace members — exact `members = [...]` list, not exclude list.
# Grab the first `members` assignment after the `[workspace]` header.
MEMBERS_RAW="$(grep -m1 '^members' Cargo.toml)"
WORKSPACE_MEMBERS=$(echo "$MEMBERS_RAW" | grep -oE '"crates/[^"]+"' | wc -l | tr -d ' ')

# Per-layer distribution from [workspace.metadata.diamond].
# `grep -c` exits 1 on zero matches under `set -e`, so we use the
# `wc -l`-on-grep-output pattern uniformly to keep the script honest.
count_layer() {
  # `pipefail` would propagate grep's exit code 1 on zero matches —
  # absorb that with `|| true` so the empty-layer case returns 0
  # instead of failing the whole script.
  # NOTE `[a-z0-9-]` (not `[a-z-]`) · crate names carry digits (nika-bm25 ·
  # nika-a11y) · the digit-blind regex undercounted L1 (showed 3 · was 5).
  { grep -E "layers\\.nika-[a-z0-9-]+ = \"$1\"\$" Cargo.toml || true; } \
    | wc -l | tr -d ' '
}
L0_COUNT=$(count_layer "L0")
L05_COUNT=$(count_layer "L0\\.5")
L1_COUNT=$(count_layer "L1")
L15_COUNT=$(count_layer "L1\\.5")
L2_COUNT=$(count_layer "L2")
L3_COUNT=$(count_layer "L3")
L4_COUNT=$(count_layer "L4")

# Identify WIP crates (admitted = passed all 12 gates; WIP = in
# workspace but pre-admission). Heuristic: nika-schema (L0 parser
# scaffolding · no admission commit yet). nika-screen + nika-ocr + nika-a11y
# ALL ADMITTED 2026-05-25 (M2.1 capture / M2.2 OCR / M2.3 a11y · ADR-003
# canonical 12 gates · mutation + Rule-2 OS-FFI/model/AXUIElement-walk
# exemptions + Foreman-direct 3-lens review per PE-5.1). Refine when a
# per-crate admission ledger lands (Phase B vector 27 candidate).
# WIP list DERIVED from the projector (single source · [workspace.metadata.diamond]
# wip = [...] in Cargo.toml) — no re-hardcoded list (per crate-spec-metrics ratchet).
WIP_CRATES="$(bash scripts/crate-metrics.sh --wip | xargs)"
WIP_COUNT=$(bash scripts/crate-metrics.sh --wip | grep -c .)
ADMITTED_COUNT=$((WORKSPACE_MEMBERS - WIP_COUNT))

# ── tests ──────────────────────────────────────────────────────────
if [[ "$QUICK" == "--quick" ]]; then
  LIB_TESTS="(skipped — pass --no-quick to compute)"
  CLIPPY_STATUS="(skipped)"
else
  LIB_TESTS=$(cargo test --workspace --lib 2>&1 | grep "test result" | awk '{sum+=$4; fail+=$6} END {print sum" passed, "fail" failed"}')
  if cargo clippy --workspace --all-targets -- -D warnings >/dev/null 2>&1; then
    CLIPPY_STATUS="0 warnings"
  else
    CLIPPY_STATUS="WARNINGS PRESENT — run cargo clippy"
  fi
fi

# ── output canonical block ─────────────────────────────────────────
cat <<EOF
<!-- AUTO-GENERATED by scripts/refresh-status.sh — do not edit by hand -->
<!-- Status drift between this block and any quoting doc is caught by
     scripts/hygiene/check-status-claims-sync.sh (vector 23). -->

| field            | value                                          |
|------------------|------------------------------------------------|
| branch           | \`$BRANCH\`                                      |
| HEAD             | \`$HEAD_SHA_SHORT\` (\`$HEAD_SHA_FULL\`)             |
| workspace        | v$WORKSPACE_VERSION                                  |
| crates (workspace)| $WORKSPACE_MEMBERS                                              |
| crates (admitted)| $ADMITTED_COUNT / 42                                   |
| crates (WIP)     | $WIP_COUNT — $WIP_CRATES                                  |
| L0               | $L0_COUNT                                              |
| L0.5             | $L05_COUNT                                              |
| L1               | $L1_COUNT                                              |
| L1.5             | $L15_COUNT                                              |
| L2               | $L2_COUNT                                              |
| L3               | $L3_COUNT                                              |
| L4               | $L4_COUNT                                              |
| lib tests        | $LIB_TESTS                              |
| clippy           | $CLIPPY_STATUS                              |
EOF
