#!/bin/bash
# Wiki health check — find issues
# Usage: ./wiki/tools/lint.sh

WIKI_DIR="$(dirname "$(dirname "$(readlink -f "$0")")")"

echo "=== Wiki Health Check ==="
echo ""

echo "-- Page count --"
find "$WIKI_DIR" -name "*.md" -not -path "*/tools/*" | wc -l
echo " total pages"
echo ""

echo "-- Orphans (not linked from other pages) --"
for f in $(find "$WIKI_DIR" -name "*.md" -not -name "index.md" -not -name "log.md" -not -name "SCHEMA.md" -not -path "*/tools/*"); do
    basename=$(basename "$f" .md)
    refs=$(grep -rl --include="*.md" "$basename" "$WIKI_DIR" 2>/dev/null | grep -v "$f" | wc -l)
    if [ "$refs" -eq 0 ]; then
        echo "  ORPHAN: $f"
    fi
done
echo ""

echo "-- Status distribution --"
for status in proven disproven evolving speculative; do
    count=$(grep -rl "status: $status" "$WIKI_DIR" --include="*.md" 2>/dev/null | wc -l)
    echo "  $status: $count"
done
echo ""

echo "-- Pages missing frontmatter --"
for f in $(find "$WIKI_DIR" -name "*.md" -not -name "SCHEMA.md" -not -path "*/tools/*"); do
    if ! head -1 "$f" | grep -q "^---"; then
        echo "  NO FRONTMATTER: $f"
    fi
done
echo ""

echo "-- Index completeness --"
indexed=$(grep -c '\[.*\](.*\.md)' "$WIKI_DIR/index.md" 2>/dev/null)
total=$(find "$WIKI_DIR" -name "*.md" -not -name "index.md" -not -name "log.md" -not -name "SCHEMA.md" -not -path "*/tools/*" | wc -l)
echo "  Indexed: $indexed / Total: $total"
echo ""

echo "=== Done ==="
