#!/usr/bin/env bash
# Verify §00 Reconstructability Invariant
# Per FVW v8 v0.10.0 §00:
# "An app is FreshVibe-compatible iff: there exists a Recipe R such that, given 
#  only R + the V4 documents, the app can be regenerated byte-for-byte."

set -e

PASS=0
FAIL=0

# Find Oscar root: walk up from script
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PACT="$(dirname "$SCRIPT_DIR")"
ROOT="$(dirname "$PACT")"

ok() { echo "  ✓ $1"; PASS=$((PASS+1)); }
err() { echo "  ✗ $1"; FAIL=$((FAIL+1)); }

echo "=== §00 Reconstructability Verification ==="
echo "PACT: $PACT"
echo "ROOT: $ROOT"
echo ""

# 1. Verify 8 mandatory artefacts
echo "[1] 8 mandatory artefacts (per §01):"

[ -f "$PACT/platform/app-pact.md" ] && ok "pact/platform/app-pact.md" || err "pact/platform/app-pact.md missing"
[ -f "$PACT/platform/invariants.md" ] && ok "pact/platform/invariants.md" || err "pact/platform/invariants.md missing"
[ -f "$PACT/platform/anti-drift.md" ] && ok "pact/platform/anti-drift.md" || err "pact/platform/anti-drift.md missing"

[ -d "$PACT/codex" ] && [ "$(ls "$PACT/codex"/*.codex.md 2>/dev/null | wc -l)" -ge 1 ] && ok "pact/codex/ ($(ls $PACT/codex/*.codex.md | wc -l) per-module C1-C9)" || err "pact/codex/ missing or empty"
NFRAGS=$(ls "$PACT/fragments"/*.md 2>/dev/null | wc -l)
[ "$NFRAGS" -ge 50 ] && ok "pact/fragments/ ($NFRAGS atomic fragment specs)" || err "pact/fragments/ insufficient ($NFRAGS < 50)"
[ -f "$PACT/dna/app.dna.json" ] && ok "pact/dna/app.dna.json (§03)" || err "pact/dna/app.dna.json missing"
[ -f "$PACT/trace-atlas/atlas.json" ] && ok "pact/trace-atlas/atlas.json (§04)" || err "pact/trace-atlas/atlas.json missing"
[ -d "$PACT/overlays" ] && ok "pact/overlays/ (§01 customisation layer)" || err "pact/overlays/ missing"
[ -d "$PACT/vp" ] && ok "pact/vp/ (§01 App-VP tests)" || err "pact/vp/ missing"
[ -d "$PACT/recipes" ] && ok "pact/recipes/ (§01 portable deliverable)" || err "pact/recipes/ missing"

# 2. Verify per-module Recipe Books (10 items each)
echo ""
echo "[2] Per-module Recipe Books (10 items each per §11):"
for MODULE in assistant brain intelligence writing data views shell ux export search onboarding branding settings; do
    MD="$PACT/recipes/$MODULE"
    if [ -f "$MD/recipe.md" ] && [ -f "$MD/codex.md" ] && [ -f "$MD/rules.md" ] \
       && [ -f "$MD/module-meta.json" ] && [ -f "$MD/ingredients.json" ] \
       && [ -f "$MD/plan.md" ] && [ -f "$MD/coverage-matrix.md" ] \
       && [ -d "$MD/diffs" ] && [ -d "$MD/trace-atlas" ] && [ -d "$MD/dna" ]; then
        ok "pact/recipes/$MODULE/ (10 items)"
    else
        err "pact/recipes/$MODULE/ incomplete"
    fi
done

# 3. Verify codex.md has C1-C9 (per §22)
echo ""
echo "[3] Codex C1-C9 content (per §22):"
for MODULE in assistant brain intelligence writing data views shell ux export search onboarding branding settings; do
    if grep -qE "^##\s*C1\b" "$PACT/codex/$MODULE.codex.md" 2>/dev/null \
       && grep -qE "^##\s*C9\b" "$PACT/codex/$MODULE.codex.md" 2>/dev/null; then
        ok "$MODULE.codex.md has C1-C9"
    else
        err "$MODULE.codex.md missing C1-C9"
    fi
done

# 4. Verify rules.md has 9 universal things (per §11.9)
echo ""
echo "[4] rules.md 9-things (per §11.9):"
for MODULE in assistant brain intelligence writing data views shell ux export search onboarding branding settings; do
    if grep -qE "^##\s*1\.\s*Capability Declaration" "$PACT/recipes/$MODULE/rules.md" 2>/dev/null \
       && grep -qE "^##\s*9\.\s*Coverage matrix" "$PACT/recipes/$MODULE/rules.md" 2>/dev/null; then
        ok "$MODULE rules.md has 9-things"
    else
        err "$MODULE rules.md missing 9-things"
    fi
done

# 5. Verify Smart-X 6 JSON files (per §16.5)
echo ""
echo "[5] Smart-X 6 JSON files (per §16.5):"
for FILE in features engines modules micro-model hooks seeds; do
    if [ -f "$PACT/recipes/smart/$FILE.json" ]; then
        ok "pact/recipes/smart/$FILE.json"
    else
        err "pact/recipes/smart/$FILE.json missing"
    fi
done

# 6. Verify DNA + Atlas well-formed
echo ""
echo "[6] DNA + Atlas well-formed:"
if python3 -c "import json; json.load(open('$PACT/dna/app.dna.json'))" 2>/dev/null; then
    ok "DNA is valid JSON"
else
    err "DNA is invalid JSON"
fi
if python3 -c "import json; json.load(open('$PACT/trace-atlas/atlas.json'))" 2>/dev/null; then
    ok "Atlas is valid JSON"
else
    err "Atlas is invalid JSON"
fi

# 7. Run App-VP tests
echo ""
echo "[7] App-VP tests:"
if [ -d "$ROOT/app/node_modules/.bin" ]; then
    cd "$ROOT/app"
    if ./node_modules/.bin/vitest run --reporter=basic 2>&1 | grep -qE "Tests[[:space:]]+[0-9]+ passed"; then
        ok "App-VP tests passing"
    else
        err "App-VP tests failing or not runnable"
    fi
else
    err "App-VP test runner not available (run npm install in app/)"
fi

# 8. Decisions log
echo ""
echo "[8] Decisions (5+ retroactive):"
DEC_COUNT=$(ls "$PACT/decisions/"DO-*.md 2>/dev/null | wc -l)
if [ "$DEC_COUNT" -ge 5 ]; then
    ok "pact/decisions/ has $DEC_COUNT decisions"
else
    err "pact/decisions/ has only $DEC_COUNT decisions (need 5+)"
fi

# Summary
echo ""
echo "=== Summary ==="
echo "PASS: $PASS"
echo "FAIL: $FAIL"
echo ""

if [ "$FAIL" -eq 0 ]; then
    echo "✓ §00 Reconstructability Invariant SATISFIED"
    echo "Oscar can be regenerated from its Recipe."
    exit 0
else
    echo "✗ §00 Reconstructability Invariant FAILED"
    echo "Drift detected per §08 Rule 7."
    exit 1
fi
