#!/usr/bin/env bash
# Wayfind — Doctor
# Validates that the memory system is correctly installed and functioning.
# Usage: bash doctor.sh [--verbose]

set -euo pipefail

VERBOSE=false
[[ "${1:-}" == "--verbose" ]] && VERBOSE=true

GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; RESET='\033[0m'

ok()   { echo -e "${GREEN}✓${RESET} $1"; }
warn() { echo -e "${YELLOW}⚠${RESET}  $1"; }
err()  { echo -e "${RED}✗${RESET} $1"; }
info() { echo "  $1"; }

ISSUES=0

is_plugin_installed() {
    # Check if wayfind is installed as a Claude Code plugin
    # Method 1: Check enabledPlugins in settings.json (authoritative — what Claude Code reads)
    local SETTINGS="$HOME/.claude/settings.json"
    if [ -f "$SETTINGS" ] && grep -q '"wayfind@' "$SETTINGS" 2>/dev/null; then
        return 0
    fi

    # Method 2: Check plugin files on disk
    local PLUGINS_DIR="$HOME/.claude/plugins"
    if [ -d "$PLUGINS_DIR" ]; then
        if find "$PLUGINS_DIR" -path '*/wayfind/plugin/.claude-plugin/plugin.json' -print -quit 2>/dev/null | grep -q .; then
            return 0
        fi
        if find "$PLUGINS_DIR" -name 'plugin.json' -exec grep -l '"name": "wayfind"' {} + 2>/dev/null | grep -q .; then
            return 0
        fi
    fi
    return 1
}

check_hook_registered() {
    echo ""
    echo "Hook registration"

    # If installed as a plugin, hooks are provided by the plugin — skip legacy checks
    if is_plugin_installed; then
        ok "Installed as Claude Code plugin (hooks provided by plugin)"
        # Warn if old specialization hook files are still present — they cause duplicate execution
        for old_hook in check-global-state.sh session-end.sh; do
            if [ -f "$HOME/.claude/hooks/$old_hook" ]; then
                warn "Orphaned legacy hook: ~/.claude/hooks/$old_hook"
                info "Plugin now handles hooks. Remove with: rm ~/.claude/hooks/$old_hook"
                ISSUES=$((ISSUES + 1))
            fi
        done
        return
    fi

    local SETTINGS="$HOME/.claude/settings.json"
    if [ ! -f "$SETTINGS" ]; then
        err "settings.json not found — hook is not registered"
        info "Run: bash setup.sh --tool claude-code"
        ISSUES=$((ISSUES + 1))
        return
    fi
    if grep -q "check-global-state" "$SETTINGS" 2>/dev/null; then
        ok "SessionStart hook registered in settings.json"
    else
        err "Hook not registered in settings.json"
        info "Re-run setup.sh to merge the hook"
        ISSUES=$((ISSUES + 1))
    fi

    if [ -f "$HOME/.claude/hooks/check-global-state.sh" ]; then
        ok "check-global-state.sh exists"
    else
        err "check-global-state.sh not found at ~/.claude/hooks/"
        ISSUES=$((ISSUES + 1))
    fi

    # Check if installed hooks are stale vs package source
    local SCRIPT_DIR
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    local SOURCE_HOOKS_DIR="$SCRIPT_DIR/specializations/claude-code/hooks"
    local INSTALLED_HOOKS_DIR="$HOME/.claude/hooks"
    for hook_file in check-global-state.sh session-end.sh; do
        local src="$SOURCE_HOOKS_DIR/$hook_file"
        local dest="$INSTALLED_HOOKS_DIR/$hook_file"
        if [ -f "$src" ] && [ -f "$dest" ]; then
            if ! diff -q "$src" "$dest" >/dev/null 2>&1; then
                warn "$hook_file is out of date — run 'wayfind update' to sync"
                ISSUES=$((ISSUES + 1))
            else
                [ "$VERBOSE" = true ] && ok "$hook_file is current"
            fi
        fi
    done

    # Validate hook structure — correct format: {matcher: str, hooks: [{type, command}]}
    if command -v python3 &>/dev/null; then
        local STRUCT_RESULT
        STRUCT_RESULT=$(python3 - "$SETTINGS" 2>/dev/null <<'PYEOF'
import json, sys
try:
    with open(sys.argv[1]) as f:
        settings = json.load(f)
except:
    print("PARSE_ERROR"); sys.exit(0)
hooks = settings.get("hooks", {})
if not isinstance(hooks, dict):
    print("BAD_HOOKS_TYPE"); sys.exit(0)
issues = []
for event in ("SessionStart", "Stop"):
    arr = hooks.get(event)
    if arr is None:
        continue
    if not isinstance(arr, list):
        issues.append(f"{event}: expected array, got {type(arr).__name__}")
        continue
    for i, group in enumerate(arr):
        if not isinstance(group, dict):
            issues.append(f"{event}[{i}]: expected object, got {type(group).__name__}")
            continue
        # Must have "hooks" array (the inner array of commands)
        if "hooks" not in group:
            if "type" in group:
                issues.append(f"{event}[{i}]: flat hook object — needs {{matcher, hooks}} wrapper")
            else:
                issues.append(f"{event}[{i}]: missing hooks array")
            continue
        if not isinstance(group["hooks"], list):
            issues.append(f"{event}[{i}].hooks: expected array, got {type(group['hooks']).__name__}")
            continue
        # Validate each hook command in the group
        for j, h in enumerate(group["hooks"]):
            if not isinstance(h, dict) or "type" not in h:
                issues.append(f"{event}[{i}].hooks[{j}]: missing type field")
if issues:
    print("MALFORMED:" + "|".join(issues))
else:
    print("OK")
PYEOF
) || STRUCT_RESULT="PARSE_ERROR"
        if [[ "$STRUCT_RESULT" == "OK" ]]; then
            ok "Hook structure valid"
        elif [[ "$STRUCT_RESULT" == MALFORMED:* ]]; then
            err "Malformed hook structure in settings.json"
            IFS='|' read -ra MSGS <<< "${STRUCT_RESULT#MALFORMED:}"
            for msg in "${MSGS[@]}"; do
                info "  $msg"
            done
            info "Fix: run 'wayfind update' to normalize hook structure"
            ISSUES=$((ISSUES + 1))
        elif [[ "$STRUCT_RESULT" == "BAD_HOOKS_TYPE" ]]; then
            err "\"hooks\" in settings.json is not an object"
            ISSUES=$((ISSUES + 1))
        elif [[ "$STRUCT_RESULT" == "PARSE_ERROR" ]]; then
            warn "Could not parse settings.json for structure validation"
        fi
    fi
}

check_global_state() {
    echo ""
    echo "Global state file"
    local GLOBAL="$HOME/.claude/global-state.md"
    if [ ! -f "$GLOBAL" ]; then
        err "global-state.md not found at $GLOBAL"
        info "Run: bash setup.sh --tool claude-code"
        ISSUES=$((ISSUES + 1))
        return
    fi
    ok "global-state.md exists"

    # Check if it was updated recently
    local LAST_UPDATED
    LAST_UPDATED=$(grep -m1 '^Last updated:' "$GLOBAL" 2>/dev/null | sed 's/^Last updated: *//' || echo "")
    if [ -n "$LAST_UPDATED" ]; then
        if [[ "$LAST_UPDATED" < "$(date -d '7 days ago' +%Y-%m-%d 2>/dev/null || date -v-7d +%Y-%m-%d 2>/dev/null || echo '0000-00-00')" ]]; then
            warn "global-state.md last updated $LAST_UPDATED (>7 days ago)"
        else
            ok "global-state.md updated $LAST_UPDATED"
        fi
    else
        warn "global-state.md has no 'Last updated:' line"
    fi
}

check_backup() {
    echo ""
    echo "Backup status"
    local LAST_PUSH="$HOME/.claude/.backup-last-push"
    local LAST_ERROR="$HOME/.claude/.backup-last-error"

    if [ ! -f "$HOME/.claude-backup/.git/config" ] 2>/dev/null && [ ! -f "$LAST_PUSH" ]; then
        info "Backup not configured (optional — run backup/setup.sh <repo-url> to enable)"
        return
    fi

    if [ -f "$LAST_ERROR" ]; then
        warn "Backup: last push FAILED"
        info "$(cat "$LAST_ERROR")"
        ISSUES=$((ISSUES + 1))
    elif [ -f "$LAST_PUSH" ]; then
        local PUSH_TIME
        PUSH_TIME=$(cat "$LAST_PUSH")
        ok "Backup: last push $PUSH_TIME"
    else
        warn "Backup configured but no push record found"
    fi
}

scan_repos() {
    echo ""
    echo "Repo state files"
    local SCAN_ROOTS="${AI_MEMORY_SCAN_ROOTS:-$HOME/repos $HOME/code $HOME/dev $HOME/projects}"
    local FOUND=0

    for root in $SCAN_ROOTS; do
        [ -d "$root" ] || continue
        while IFS= read -r state_file; do
            FOUND=$((FOUND + 1))
            local rel="${state_file#$HOME/}"
            local repo_name="${rel%/.claude/team-state.md}"
            repo_name="${repo_name%/.claude/state.md}"
            local LAST_UPDATED
            LAST_UPDATED=$(grep -m1 '^Last updated:' "$state_file" 2>/dev/null | sed 's/^Last updated: *//' || echo "unknown")
            ok "$repo_name (updated $LAST_UPDATED)"
        done < <(find "$root" \( -path '*/.claude/state.md' -o -path '*/.claude/team-state.md' \) 2>/dev/null | sort)
    done

    if [ "$FOUND" -eq 0 ]; then
        info "No repos with state.md found in: $SCAN_ROOTS"
        info "Run /init-memory in a repo to initialize it"
    fi
}

check_memory_files() {
    echo ""
    echo "Memory files"
    local MEMORY_DIR="$HOME/.claude/memory"
    if [ ! -d "$MEMORY_DIR" ]; then
        warn "Memory directory not found: $MEMORY_DIR"
        return
    fi

    local COUNT=0
    while IFS= read -r f; do
        COUNT=$((COUNT + 1))
        local SIZE
        SIZE=$(wc -c < "$f" 2>/dev/null || echo 0)
        local SIZE_KB=$((SIZE / 1024))
        local FNAME
        FNAME=$(basename "$f")
        if [ "$SIZE" -gt 8192 ]; then
            warn "$FNAME (${SIZE_KB}KB) — exceeds recommended 8KB; consider summarizing"
        else
            [ "$VERBOSE" = true ] && ok "$FNAME (${SIZE_KB}KB)"
        fi
    done < <(find "$MEMORY_DIR" -name '*.md' -not -path '*/journal/*' 2>/dev/null | sort)

    local JOURNAL_DIR="$MEMORY_DIR/journal"
    if [ -d "$JOURNAL_DIR" ]; then
        local JOURNAL_COUNT
        JOURNAL_COUNT=$(find "$JOURNAL_DIR" -name '*.md' 2>/dev/null | wc -l)
        ok "Journal: $JOURNAL_COUNT entries"
    fi

    [ "$COUNT" -gt 0 ] && ok "$COUNT memory file(s) found" || info "No memory files yet"
}

check_team_context_freshness() {
    echo ""
    echo "Team context sync"

    # Find team-context path via wayfind CLI
    local TEAM_PATH=""
    if command -v wayfind >/dev/null 2>&1; then
        TEAM_PATH=$(wayfind context show 2>/dev/null | grep 'Path:' | head -1 | sed 's/.*Path: *//' || true)
    fi

    if [ -z "$TEAM_PATH" ] || [ ! -d "$TEAM_PATH" ]; then
        info "No team-context repo configured (optional)"
        return
    fi

    local MARKER="$TEAM_PATH/.last-pull"
    if [ ! -f "$MARKER" ]; then
        # No pull has ever succeeded — check if this is a fresh install
        # by looking at the git log age instead
        local LAST_COMMIT
        LAST_COMMIT=$(git -C "$TEAM_PATH" log -1 --format='%ci' 2>/dev/null || echo "")
        if [ -z "$LAST_COMMIT" ]; then
            info "Team-context repo exists but has no commits"
        else
            ok "Team-context configured (pull marker will appear after next session start)"
        fi
        return
    fi

    local PULL_TIME
    PULL_TIME=$(cat "$MARKER")
    local PULL_EPOCH
    PULL_EPOCH=$(date -d "$PULL_TIME" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%S" "${PULL_TIME%%.*}" +%s 2>/dev/null || echo 0)
    local NOW_EPOCH
    NOW_EPOCH=$(date +%s)
    local AGE_HOURS=$(( (NOW_EPOCH - PULL_EPOCH) / 3600 ))

    if [ "$AGE_HOURS" -gt 24 ]; then
        warn "Team-context last pulled ${AGE_HOURS}h ago — other engineers' context may be stale"
        info "Run: wayfind context pull"
        ISSUES=$((ISSUES + 1))
    else
        ok "Team-context pulled ${AGE_HOURS}h ago"
    fi
}

check_team_versions() {
    echo ""
    echo "Team version compliance"

    # Get installed version from .wayfind-version file (written by setup.sh)
    local VERSION_FILE="$HOME/.claude/team-context/.wayfind-version"
    local INSTALLED="unknown"
    if [ -f "$VERSION_FILE" ]; then
        INSTALLED=$(cat "$VERSION_FILE")
    fi

    if [ "$INSTALLED" = "unknown" ]; then
        info "Version not recorded yet (run wayfind update to set)"
        return
    fi

    ok "Installed version: v$INSTALLED"

    # Check min_version from check-version command output
    local CHECK_OUTPUT=""
    if command -v wayfind >/dev/null 2>&1; then
        CHECK_OUTPUT=$(wayfind check-version 2>&1 || true)
    fi

    if echo "$CHECK_OUTPUT" | grep -q "below team minimum"; then
        local MIN_VER
        MIN_VER=$(echo "$CHECK_OUTPUT" | grep -o 'minimum v[0-9.]*' | head -1 | sed 's/minimum v//')
        warn "Below team minimum v$MIN_VER — run: npm update -g wayfind"
        ISSUES=$((ISSUES + 1))
    else
        ok "Version meets team requirements"
    fi
}

check_storage_backend() {
    echo ""
    echo "Storage backend"

    set +e  # Temporarily disable errexit — node calls may fail in test environments
    # Check for env var override
    if [ -n "${TEAM_CONTEXT_STORAGE_BACKEND:-}" ]; then
        info "TEAM_CONTEXT_STORAGE_BACKEND=$TEAM_CONTEXT_STORAGE_BACKEND (env override)"
    fi

    local SCRIPT_DIR
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

    # Resolve the team-scoped store path (respects .claude/wayfind.json + context.json default)
    local RESOLVED_STORE ACTIVE_TEAM_ID
    RESOLVED_STORE=$(node -e "
      try {
        const cs = require('$SCRIPT_DIR/bin/content-store.js');
        console.log(cs.resolveStorePath());
      } catch (e) {
        console.log('SKIP:' + e.message.split('\n')[0]);
      }
    " 2>/dev/null) || RESOLVED_STORE="SKIP:content-store not available"
    ACTIVE_TEAM_ID=$(node -e "
      try {
        const cs = require('$SCRIPT_DIR/bin/content-store.js');
        // resolveStorePath triggers team ID resolution; extract from path
        const p = cs.resolveStorePath();
        const m = p && p.match(/teams\/([^/]+)\/content-store/);
        console.log(m ? m[1] : 'default');
      } catch (e) {
        console.log('unknown');
      }
    " 2>/dev/null) || ACTIVE_TEAM_ID="unknown"

    if [[ "$RESOLVED_STORE" != SKIP:* ]]; then
        ok "Active team: $ACTIVE_TEAM_ID"
        info "Store path: $RESOLVED_STORE"
    fi

    local WAYFIND_DIR="${WAYFIND_DIR:-$HOME/.claude/team-context}"

    # ── 1. Which backend is active ───────────────────────────────────────────
    local BACKEND_RESULT=""
    BACKEND_RESULT=$(node -e "
      try {
        const cs = require('$SCRIPT_DIR/bin/content-store.js');
        const storage = require('$SCRIPT_DIR/bin/storage/index.js');
        const storePath = cs.resolveStorePath();
        storage.getBackend(storePath);
        const info = storage.getBackendInfo(storePath);
        if (!info) { console.log('NONE'); process.exit(0); }
        console.log(JSON.stringify(info));
      } catch (e) {
        console.log('SKIP:' + e.message.split('\n')[0]);
      }
    " 2>/dev/null) || BACKEND_RESULT="SKIP:storage module not available"

    if [[ "$BACKEND_RESULT" == SKIP:* ]]; then
        [ "$VERBOSE" = true ] && info "Storage check skipped: ${BACKEND_RESULT#SKIP:}"
    elif [[ "$BACKEND_RESULT" == "NONE" ]]; then
        info "No storage backend initialized (team-context not configured)"
    else
        local BACKEND_TYPE
        BACKEND_TYPE=$(echo "$BACKEND_RESULT" | node -e "const d=JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')); console.log(d.type)" 2>/dev/null || echo "unknown")
        local IS_FALLBACK
        IS_FALLBACK=$(echo "$BACKEND_RESULT" | node -e "const d=JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')); console.log(d.fallback)" 2>/dev/null || echo "false")

        if [ "$BACKEND_TYPE" = "sqlite" ]; then
            ok "Active backend: sqlite"
        elif [ "$BACKEND_TYPE" = "json" ]; then
            ok "Active backend: json"
        else
            warn "Active backend: $BACKEND_TYPE (unexpected)"
        fi

        # ── 2. Fallback detection ────────────────────────────────────────────
        if [ "$IS_FALLBACK" = "true" ]; then
            # JSON fallback is expected when better-sqlite3 isn't available (optional dep).
            # Warn only — don't increment ISSUES. Use `wayfind doctor --container` in Docker.
            warn "JSON backend (better-sqlite3 unavailable — optional)"
            info "To enable SQLite: npm install -g better-sqlite3  (or rebuild container image)"
        elif [ "$BACKEND_TYPE" = "json" ]; then
            # Not a runtime fallback — check if sqlite3 is available but unused
            if node -e "require('better-sqlite3')" 2>/dev/null; then
                warn "better-sqlite3 is installed but backend is JSON — possible unexpected fallback"
                info "Check TEAM_CONTEXT_STORAGE_BACKEND env var or re-run setup"
                ISSUES=$((ISSUES + 1))
            else
                [ "$VERBOSE" = true ] && info "better-sqlite3 not installed — JSON is expected"
            fi
        fi
    fi

    # ── 3. Signal freshness (connectors.json last_pull) ──────────────────────
    local CONNECTORS="$WAYFIND_DIR/connectors.json"
    if [ -f "$CONNECTORS" ]; then
        local FRESHNESS_RESULT
        FRESHNESS_RESULT=$(node -e "
          const fs = require('fs');
          const config = JSON.parse(fs.readFileSync('$CONNECTORS', 'utf8'));
          const connectors = ['github', 'intercom', 'notion'];
          const now = Date.now();
          const DAY = 24 * 60 * 60 * 1000;
          const results = [];
          for (const name of connectors) {
            const c = config[name];
            if (!c) continue;
            const lp = c.last_pull;
            if (!lp) {
              results.push('WARN:' + name + ':never pulled');
            } else {
              const age = now - new Date(lp).getTime();
              const hours = Math.floor(age / (60 * 60 * 1000));
              if (age > DAY) {
                results.push('WARN:' + name + ':' + hours + 'h ago');
              } else {
                results.push('OK:' + name + ':' + hours + 'h ago');
              }
            }
          }
          console.log(results.join('|'));
        " 2>/dev/null) || FRESHNESS_RESULT=""

        if [ -n "$FRESHNESS_RESULT" ]; then
            IFS='|' read -ra ENTRIES <<< "$FRESHNESS_RESULT"
            for entry in "${ENTRIES[@]}"; do
                local STATUS="${entry%%:*}"
                local REST="${entry#*:}"
                local NAME="${REST%%:*}"
                local DETAIL="${REST#*:}"
                if [ "$STATUS" = "WARN" ]; then
                    warn "Signal $NAME: $DETAIL"
                    info "Run: wayfind pull $NAME"
                    ISSUES=$((ISSUES + 1))
                else
                    ok "Signal $NAME: last pull $DETAIL"
                fi
            done
        fi
    else
        [ "$VERBOSE" = true ] && info "No connectors.json — signal freshness check skipped"
    fi

    # ── 4. Embedding coverage ───────────────────────────────────────────────
    local EMBED_RESULT
    EMBED_RESULT=$(node -e "
      try {
        const cs = require('$SCRIPT_DIR/bin/content-store.js');
        const storage = require('$SCRIPT_DIR/bin/storage/index.js');
        const storePath = cs.resolveStorePath();
        const backend = storage.getBackend(storePath);
        const idx = backend.loadIndex();
        if (!idx || !idx.entries) { console.log('SKIP'); process.exit(0); }
        let total = 0, embedded = 0;
        for (const e of Object.values(idx.entries)) { total++; if (e.hasEmbedding) embedded++; }
        if (total === 0) { console.log('SKIP'); process.exit(0); }
        const pct = Math.round(100 * embedded / total);
        const hasKey = !!(process.env.OPENAI_API_KEY || process.env.AZURE_OPENAI_EMBEDDING_ENDPOINT);
        console.log(pct + ':' + embedded + ':' + total + ':' + hasKey);
      } catch (e) {
        console.log('SKIP:' + e.message.split('\n')[0]);
      }
    " 2>/dev/null) || EMBED_RESULT="SKIP"

    if [[ "$EMBED_RESULT" != SKIP* ]]; then
        IFS=':' read -r PCT EMBEDDED TOTAL HAS_KEY <<< "$EMBED_RESULT"
        if [ "$PCT" -ge 90 ]; then
            ok "Embeddings: $EMBEDDED/$TOTAL entries ($PCT%)"
        elif [ "$PCT" -ge 50 ]; then
            warn "Embeddings: only $EMBEDDED/$TOTAL entries ($PCT%) — search quality degraded"
            info "Run: wayfind reindex  (with OPENAI_API_KEY or AZURE_OPENAI_EMBEDDING_* set)"
            ISSUES=$((ISSUES + 1))
        elif [ "$PCT" -gt 0 ]; then
            err "Embeddings: $EMBEDDED/$TOTAL entries ($PCT%) — semantic search mostly broken"
            info "Run: wayfind reindex  (with OPENAI_API_KEY or AZURE_OPENAI_EMBEDDING_* set)"
            ISSUES=$((ISSUES + 1))
        else
            if [ "$HAS_KEY" = "true" ]; then
                err "Embeddings: 0/$TOTAL entries — embedding API key is set but no embeddings generated"
                info "Run: wayfind reindex  to generate embeddings"
                ISSUES=$((ISSUES + 1))
            else
                warn "Embeddings: 0/$TOTAL entries — no embedding API key configured"
                info "Set OPENAI_API_KEY or AZURE_OPENAI_EMBEDDING_* then run: wayfind reindex"
                ISSUES=$((ISSUES + 1))
            fi
        fi
    fi

    set -e  # Restore errexit
}

# Run checks
echo ""
echo "Wayfind — Doctor"
echo "══════════════════════"

check_hook_registered
check_global_state
check_backup
check_team_context_freshness
check_team_versions
check_storage_backend
check_memory_files
scan_repos

echo ""
echo "══════════════════════"
if [ "$ISSUES" -eq 0 ]; then
    echo -e "${GREEN}All checks passed${RESET}"
else
    echo -e "${YELLOW}$ISSUES issue(s) found${RESET}"
    exit 1
fi
