#!/usr/bin/env bash
# verify-state.sh — machine-checkable state verification (S1–S9).
# Usage: ./scripts/verify-state.sh [project-root]
#
# Thin wrapper: all checks live in the qatro CLI (`qatro verify`),
# selected by the "core_version" pinned in feature_list.json. Set
# QATRO_BIN to override the launcher (used by tests and by the
# qatrolabs-core repo itself).
#
# Canonical copy: qatrolabs-core/scripts/verify-state.sh. Project
# instances are verbatim copies; scripts/verify.sh checks they haven't
# drifted.
set -u
RED='\033[0;31m'; NC='\033[0m'
fail() { printf "${RED}[FAIL]${NC}  %s\n" "$1"; }

ROOT="${1:-.}"
if [ ! -f "$ROOT/feature_list.json" ]; then
  fail "no feature_list.json in '$ROOT' — run from a project root or pass it as \$1"
  exit 1
fi
cd "$ROOT" || exit 1

if [ -n "${QATRO_BIN:-}" ]; then
  # Intentional word-split: QATRO_BIN may be "node /path/qatro.js".
  exec $QATRO_BIN verify
fi
if ! command -v pnpm >/dev/null 2>&1; then
  fail "pnpm is required (or set QATRO_BIN to a qatro launcher)"
  exit 1
fi
CORE_VERSION=$(sed -n 's/.*"core_version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' feature_list.json | head -n 1)
if [ -z "$CORE_VERSION" ]; then
  fail 'no "core_version" in feature_list.json — run qatro upgrade first'
  exit 1
fi
exec pnpm dlx "@qatrolabs/qatro@${CORE_VERSION}" verify
