#!/usr/bin/env bash
set -Eeuo pipefail

BASE="/home/yeff/public_html/devon"
CANON="$BASE/canon"
DATA="$BASE/panel/data"
DOCS="$BASE/docs/index.php"
HUB="$DATA/hub_index.json"
MASTER="$DATA/master_architecture_index.md"
CONTINUITY="$DATA/devon_continuity.md"

print_file() {
  local f="$1"
  if [ -f "$f" ]; then
    echo
    echo "----- $(basename "$f") -----"
    cat "$f"
  else
    echo
    echo "----- $(basename "$f") -----"
    echo "MISSING: $f"
  fi
}

echo "===== CANON ====="
for f in \
  "$CANON/03_RULES_OF_OPERATION.md" \
  "$CANON/06_DECISIONS.md" \
  "$CANON/07_SCOPE.md" \
  "$CANON/08_NEXT_ACTION.md"
do
  print_file "$f"
done

echo
echo "===== CORE ARCHITECTURE ====="
if [ -f "$MASTER" ]; then
  cat "$MASTER"
else
  echo "MISSING: $MASTER"
fi

echo
echo "===== CONTINUITY ====="
if [ -f "$CONTINUITY" ]; then
  cat "$CONTINUITY"
else
  echo "MISSING: $CONTINUITY"
fi

echo
echo "===== DOCUMENTATION HUB SNAPSHOT ====="
if [ -f "$HUB" ]; then
  python3 - <<'PY'
from pathlib import Path
import json

p = Path("/home/yeff/public_html/devon/panel/data/hub_index.json")
data = json.loads(p.read_text())

phases = data.get("phases", [])
cats = data.get("categories", [])

print("PHASE COUNT:", len(phases))
for ph in phases:
    print(f"- {ph.get('id')} | {ph.get('label')}")

print()
print("CATEGORY COUNT:", len(cats))
for cat in cats:
    docs = cat.get("docs", [])
    print(f"CATEGORY: {cat.get('id')} | LABEL: {cat.get('label')} | PHASE: {cat.get('phase')} | DOCS: {len(docs)}")
    for doc in docs:
        print(f"  DOC: {doc.get('id')} | LABEL: {doc.get('label')} | TYPE: {doc.get('type')}")
    print()
PY
else
  echo "MISSING: $HUB"
fi

echo
echo "===== PHASE 01 + PHASE 02 DOC MAP ====="
if [ -f "$HUB" ]; then
  python3 - <<'PY'
from pathlib import Path
import json

p = Path("/home/yeff/public_html/devon/panel/data/hub_index.json")
data = json.loads(p.read_text())

wanted = {"phase_01", "phase_02"}
for cat in data.get("categories", []):
    if cat.get("phase") in wanted:
        print(f"[{cat.get('phase')}] {cat.get('id')} => {cat.get('label')}")
        for doc in cat.get("docs", []):
            print(f"  - {doc.get('id')} => {doc.get('label')}")
        print()
PY
else
  echo "MISSING: $HUB"
fi

echo
echo "===== DOCS INDEX CUSTOM BRANCHES ====="
if [ -f "$DOCS" ]; then
  grep -n 'if (doc.id === "' "$DOCS" || true
else
  echo "MISSING: $DOCS"
fi

echo
echo "===== DOCS INDEX forceDirectArchitectureRender ====="
if [ -f "$DOCS" ]; then
  python3 - <<'PY'
from pathlib import Path

p = Path("/home/yeff/public_html/devon/docs/index.php")
s = p.read_text()

needle = "const forceDirectArchitectureRender ="
i = s.find(needle)
if i == -1:
    print("MISSING: forceDirectArchitectureRender block")
else:
    j = s.find("if (!forceDirectArchitectureRender)", i)
    if j == -1:
        j = min(len(s), i + 1800)
    print(s[i:j].rstrip())
PY
else
  echo "MISSING: $DOCS"
fi

echo
echo "===== DOCS INDEX PHASE STATUS SNAPSHOT ====="
if [ -f "$DOCS" ] && [ -f "$HUB" ]; then
  python3 - <<'PY'
from pathlib import Path
import json
import re

docs_path = Path("/home/yeff/public_html/devon/docs/index.php")
hub_path = Path("/home/yeff/public_html/devon/panel/data/hub_index.json")

s = docs_path.read_text()
hub = json.loads(hub_path.read_text())

branches = set()
for m in re.finditer(r'if \(doc\.id === "([^"]+)" && state\.categoryId === "([^"]+)"\)', s):
    branches.add((m.group(1), m.group(2)))

for phase in ("phase_01", "phase_02"):
    print(f"{phase.upper()}:")
    for cat in hub.get("categories", []):
        if cat.get("phase") != phase:
            continue
        cat_id = cat.get("id")
        print(f"  CATEGORY: {cat_id} | {cat.get('label')}")
        for doc in cat.get("docs", []):
            doc_id = doc.get("id")
            status = "CUSTOM_BRANCH" if (doc_id, cat_id) in branches else "GENERIC_RENDER"
            print(f"    - {doc_id}: {status}")
    print()
PY
else
  echo "MISSING: $DOCS or $HUB"
fi

echo
echo "===== DOCS INDEX SYNTAX ====="
if [ -f "$DOCS" ]; then
  php -l "$DOCS" || true
else
  echo "MISSING: $DOCS"
fi

echo
echo "===== DUMP STATUS ====="
echo "PASS: canonical + docs + hub context reconstructed from server evidence"
