#!/usr/bin/env bash
# CHECK-draft-verification-report-2026-11-02
# Re-derives the draft-compliance scan and verifies the report's claims.
set -euo pipefail

LANE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPORT="$LANE/draft-verification-report-2026-11-02.md"
TSV="/tmp/verify-2026-11-02-check.tsv"

if [ ! -f "$REPORT" ]; then
    echo "MISSING report: $REPORT" >&2
    exit 1
fi

cd "$LANE"
python3 draft_compliance_report.py --out "$TSV"

# Skip comment line (# measured...) and header line.
total=$(tail -n +3 "$TSV" | wc -l)
has_verdict=$(awk -F'\t' '$3 == "True" {c++} END {print c+0}' "$TSV")
has_check=$(awk -F'\t' '$4 == "True" {c++} END {print c+0}' "$TSV")
check_clean=$(awk -F'\t' '$5 == "True" {c++} END {print c+0}' "$TSV")
both=$(awk -F'\t' '$3 == "True" && $5 == "True" {c++} END {print c+0}' "$TSV")

ok=0
[ "$total" -eq 94 ] || { echo "FAIL total: got $total, want 94" >&2; ok=1; }
[ "$has_verdict" -eq 22 ] || { echo "FAIL has_verdict: got $has_verdict, want 22" >&2; ok=1; }
[ "$has_check" -eq 19 ] || { echo "FAIL has_check: got $has_check, want 19" >&2; ok=1; }
[ "$check_clean" -eq 16 ] || { echo "FAIL check_clean: got $check_clean, want 16" >&2; ok=1; }
[ "$both" -eq 16 ] || { echo "FAIL both: got $both, want 16" >&2; ok=1; }
grep -q "VERDICT: 16/94" "$REPORT" || { echo "FAIL verdict line missing" >&2; ok=1; }

exit "$ok"
