#!/usr/bin/bash
# lamboot-doctor — guided diagnose → repair → verify wrapper
#
# The "it just works" entry point. Orchestrates sibling tools per a
# conservative default-action policy matrix. Users type sudo ONCE.
#
# Part of the lamboot-tools suite (SPEC-LAMBOOT-TOOLKIT-V1.md §6.7).

# shellcheck source=../lib/lamboot-toolkit-lib.sh
source "${LAMBOOT_LIB_DIR:-/usr/lib/lamboot-tools}/lamboot-toolkit-lib.sh"
# shellcheck source=../lib/lamboot-toolkit-help.sh
source "${LAMBOOT_LIB_DIR:-/usr/lib/lamboot-tools}/lamboot-toolkit-help.sh"
# shellcheck source=../lib/lamboot-capcheck-bridge.sh
# Soft-include the capcheck bridge — degrades gracefully if missing.
if [[ -f "${LAMBOOT_LIB_DIR:-/usr/lib/lamboot-tools}/lamboot-capcheck-bridge.sh" ]]; then
    # shellcheck disable=SC1091
    source "${LAMBOOT_LIB_DIR:-/usr/lib/lamboot-tools}/lamboot-capcheck-bridge.sh"
fi

LAMBOOT_TOOL_NAME="lamboot-doctor"
LAMBOOT_TOOL_VERSION="0.8.0"

readonly DOC_BASE="https://github.com/lamco-admin/lamboot-tools"
readonly STATE_DIR="/var/lib/lamboot-doctor"

OPT_NO_REPAIR=0
OPT_NO_CLEAN=0
OPT_RISK_LIMIT="moderate"
OPT_RESUME_ID=""
OPT_KEEP_RESUME=0
OPT_SUBCOMMAND="check"
OPT_CLEAN_APPLY=0
OPT_ONLY=""
OPT_BACKUP_DIR=""
OPT_CLEAN_CATEGORY=""

DOCTOR_CLEAN_LOG="/var/log/lamboot/lamboot-doctor.log"

# Tallies
DOCTOR_APPLIED=0
DOCTOR_FAILED=0
DOCTOR_SKIPPED=0

# Plan steps (parallel arrays)
PLAN_FINDING_IDS=()
PLAN_SEVERITIES=()
PLAN_ACTIONS=()  # e.g. "lamboot-repair --auto" or "lamboot-esp clean"
PLAN_POLICIES=() # "confirm" | "auto" | "manual" | "skip"

# ── Help registry ────────────────────────────────────────────────────────

_register_subcommands() {
    register_subcommand \
        --name "check" \
        --category "Diagnostics" \
        --summary "Guided boot health check + repair (default action)" \
        --syntax "lamboot-doctor [--auto] [--risk-limit LEVEL] [--no-repair] [--no-clean] [--offline DISK] [--json]" \
        --arg "--auto:Non-interactive; still confirms typed-yes for critical findings" \
        --arg "--risk-limit LEVEL:safe|moderate|destructive (default: moderate)" \
        --arg "--no-repair:Diagnose + show plan; don't invoke repair" \
        --arg "--no-clean:Skip ESP clean actions even if they would apply" \
        --arg "--offline DISK:Operate against a disk image; propagates to diagnose/repair/esp/backup" \
        --example "sudo lamboot-doctor" \
        --example "lamboot-doctor --no-repair" \
        --example "sudo lamboot-doctor --auto" \
        --example "lamboot-doctor --offline /dev/pve/vm-201-disk-1 --no-repair" \
        --example "lamboot-doctor --json | jq .context.plan" \
        --notes "Chains lamboot-diagnose → policy matrix → lamboot-repair / lamboot-esp clean. Never invokes lamboot-migrate automatically (always user-initiated). Sudo escalation happens at most once per run. Offline mode propagates the --offline DISK flag to every sub-tool invocation; the disk is attached once by diagnose, findings guide offline-safe repairs, and teardown happens automatically at exit." \
        --offline-capable "true" \
        --requires-root "false" \
        --see-also "lamboot-diagnose, lamboot-repair, lamboot-esp, lamboot-migrate" \
        --doc-url "https://github.com/lamco-admin/lamboot-tools" \
        --maturity "beta"

    register_subcommand \
        --name "clean" --category "Maintenance" \
        --summary "Guided, reversible cleanup of ESP and NVRAM (dry-run by default)" \
        --syntax "lamboot-doctor clean [--apply] [--only esp,nvram,boot] [--category CAT] [--backup-dir DIR] [--json] [--yes]" \
        --arg "--apply:Perform the cleanup (default: preview only)" \
        --arg "--only LIST:Restrict to surfaces: esp, nvram, boot (default: esp,nvram)" \
        --arg "--category CAT:Pass a hygiene category through to the worker (e.g. duplicate)" \
        --arg "--backup-dir DIR:Override the backup directory (default: /var/backups/lamboot-doctor-<ts>)" \
        --example "lamboot-doctor clean" \
        --example "sudo lamboot-doctor clean --only nvram --apply" \
        --notes "Orchestrates lamboot-nvram clean (NVRAM) and lamboot-esp clean (ESP files). Backs up before --apply (NVRAM via lamboot-nvram; ESP files copied into the backup dir by doctor) and logs every action to /var/log/lamboot/. Reverse with 'lamboot-doctor restore'. /boot kernel removal is package-manager-deferential (reported, never auto-removed). Never removes the running kernel, active bootloader, or current/default boot entry." \
        --offline-capable "false" --requires-root "true" \
        --see-also "restore, lamboot-nvram, lamboot-esp" \
        --doc-url "https://github.com/lamco-admin/lamboot-tools" --maturity "draft"

    register_subcommand \
        --name "restore" --category "Maintenance" \
        --summary "Reverse a previous 'clean' from its backup directory" \
        --syntax "lamboot-doctor restore [--backup-dir DIR] [--only esp,nvram] [--json] [--yes]" \
        --arg "--backup-dir DIR:Backup to restore (default: most recent /var/backups/lamboot-doctor-*)" \
        --arg "--only LIST:Restrict restore to surfaces: esp, nvram" \
        --example "sudo lamboot-doctor restore" \
        --notes "Reads MANIFEST.json in the backup dir, copies removed ESP files back, and runs 'lamboot-nvram restore' to re-create removed boot entries. Idempotent; logs every action." \
        --offline-capable "false" --requires-root "true" \
        --see-also "clean" \
        --doc-url "https://github.com/lamco-admin/lamboot-tools" --maturity "draft"
}

# ── Remediation helper ───────────────────────────────────────────────────

_remediation_json() {
    local id="$1" summary="$2" cmd="${3:-}"
    local doc="${DOC_BASE}/${id//.//}"
    local cmd_field="\"\""
    [[ -n "$cmd" ]] && cmd_field="\"$(json_escape "$cmd")\""
    printf '{"summary":"%s","command":%s,"doc_url":"%s"}' \
        "$(json_escape "$summary")" "$cmd_field" "$doc"
}

# ── Sibling tool resolution ──────────────────────────────────────────────

_find_sibling() {
    local tool="$1"

    if command -v "$tool" >/dev/null 2>&1; then
        command -v "$tool"
        return 0
    fi

    # Dev-tree fallback: same directory as this tool
    local script_dir
    script_dir=$(cd "$(dirname "$0")" && pwd)
    if [[ -x "${script_dir}/${tool}" ]]; then
        printf '%s' "${script_dir}/${tool}"
        return 0
    fi

    return 1
}

require_siblings() {
    local missing=()
    for tool in lamboot-diagnose lamboot-repair lamboot-esp; do
        _find_sibling "$tool" >/dev/null 2>&1 || missing+=("$tool")
    done
    if [[ ${#missing[@]} -gt 0 ]]; then
        die_prerequisite "sibling tools not found: ${missing[*]}; install the lamboot-tools package"
    fi
}

# ── Phase: run diagnose ──────────────────────────────────────────────────

run_diagnose() {
    local diagnose_bin
    diagnose_bin=$(_find_sibling lamboot-diagnose) || die_prerequisite "lamboot-diagnose not found"

    local output
    # Preserve LAMBOOT_LIB_DIR across fork when running in dev tree.
    # Propagate --offline DISK if the user supplied one; each sub-tool invocation
    # runs its own offline_setup/teardown cycle against the same disk (readonly
    # mounts, so concurrent attachment is safe across serial sub-tool calls).
    if [[ -n "$LAMBOOT_OFFLINE_DISK" ]]; then
        output=$(LAMBOOT_LIB_DIR="${LAMBOOT_LIB_DIR:-}" "$diagnose_bin" \
                    --offline "$LAMBOOT_OFFLINE_DISK" --json 2>/dev/null || true)
    else
        output=$(LAMBOOT_LIB_DIR="${LAMBOOT_LIB_DIR:-}" "$diagnose_bin" --json 2>/dev/null || true)
    fi

    if [[ -z "$output" ]]; then
        die "lamboot-diagnose produced no output"
    fi

    printf '%s' "$output"
}

# ── Plan: translate findings into actions per policy matrix ──────────────

_severity_of() {
    printf '%s' "$1" | grep -oE '"severity":"[^"]*"' | head -1 | cut -d'"' -f4
}

_id_of() {
    printf '%s' "$1" | grep -oE '"id":"[^"]*"' | head -1 | cut -d'"' -f4
}

_title_of() {
    printf '%s' "$1" | grep -oE '"title":"[^"]*"' | head -1 | cut -d'"' -f4
}

_remediation_command_of() {
    printf '%s' "$1" | grep -oE '"remediation":\{[^}]*\}' | head -1 \
        | grep -oE '"command":"[^"]*"' | head -1 | cut -d'"' -f4
}

# Pulled from capcheck-bridge-spliced findings. Values: "safe", "moderate",
# "destructive", "none", or "" when absent.
_remediation_risk_of() {
    printf '%s' "$1" | grep -oE '"remediation":\{[^}]*\}' | head -1 \
        | grep -oE '"risk":"[^"]*"' | head -1 | cut -d'"' -f4
}

build_plan_from_findings() {
    local findings_blob="$1"

    # Split into individual finding objects. They're comma-joined; replace },{ with }\n{
    local split
    split=$(printf '%s' "$findings_blob" | sed 's/},{/}\n{/g')

    while IFS= read -r finding; do
        [[ -z "$finding" ]] && continue

        local id sev title cmd risk
        id=$(_id_of "$finding")
        sev=$(_severity_of "$finding")
        title=$(_title_of "$finding")
        cmd=$(_remediation_command_of "$finding")
        risk=$(_remediation_risk_of "$finding")

        [[ -z "$id" ]] && continue

        # Info severity: not surfaced
        [[ "$sev" == "info" ]] && continue

        # --risk-limit gate (capcheck-spliced findings carry an explicit risk).
        # safe       → only safe actions auto/confirm; moderate/destructive → manual
        # moderate   → safe + moderate auto/confirm; destructive → manual
        # destructive → all allowed
        local risk_gated=0
        case "$OPT_RISK_LIMIT" in
            safe)
                if [[ "$risk" == "moderate" || "$risk" == "destructive" ]]; then
                    risk_gated=1
                fi
                ;;
            moderate)
                if [[ "$risk" == "destructive" ]]; then
                    risk_gated=1
                fi
                ;;
        esac

        # Default policy per matrix (§3)
        local policy="skip" action=""

        case "$sev" in
            critical)
                policy="confirm"
                ;;
            error)
                if [[ $LAMBOOT_AUTO -eq 1 ]]; then
                    policy="auto"
                else
                    policy="confirm"
                fi
                ;;
            warning)
                policy="manual"
                ;;
        esac

        # Exception: migrate recommendations are never auto-invoked
        if [[ "$cmd" == *"lamboot-migrate"* ]]; then
            policy="manual"
        fi

        # Exception: partition_table.type is manual regardless
        if [[ "$id" == "partition_table.type" ]]; then
            policy="manual"
        fi

        # Exception: esp.stale.* needs --auto AND below-10% space to auto-apply
        if [[ "$id" == esp.stale.* ]] && [[ $OPT_NO_CLEAN -eq 1 ]]; then
            policy="skip"
        fi

        # Map the remediation command to a safe sibling invocation
        action=$(_map_action_from_command "$cmd" "$id")

        # If we can't map it, downgrade to manual (print only)
        if [[ -z "$action" ]]; then
            policy="manual"
        fi

        # Risk-limit downgrade — manual-print the gated action.
        if [[ $risk_gated -eq 1 ]]; then
            policy="manual"
            verbose "risk-gated: $id (risk=$risk, limit=$OPT_RISK_LIMIT)"
        fi

        # --no-repair: downgrade every would-be-executed step to manual
        if [[ $OPT_NO_REPAIR -eq 1 ]]; then
            [[ "$policy" == "auto" ]] || [[ "$policy" == "confirm" ]] && policy="manual"
        fi

        PLAN_FINDING_IDS+=("$id")
        PLAN_SEVERITIES+=("$sev")
        PLAN_ACTIONS+=("$action")
        PLAN_POLICIES+=("$policy")
    done <<< "$split"
}

# Translate a remediation.command string into a doctor-safe sibling invocation.
# Returns the command to run, or empty if we can't or won't run it.
_map_action_from_command() {
    local cmd="$1"
    local id="$2"

    # Strip leading "sudo " if present
    cmd="${cmd#sudo }"

    # Propagate --offline DISK to any sub-tool invocation we generate.
    # esp clean is suppressed entirely in offline mode — the action is unsafe
    # without live NVRAM sync and the stale-file detector needs runtime context.
    local offline_suffix=""
    if [[ -n "$LAMBOOT_OFFLINE_DISK" ]]; then
        offline_suffix=" --offline $LAMBOOT_OFFLINE_DISK"
    fi

    case "$cmd" in
        "")
            printf ''
            ;;
        "lamboot-repair --fix-fstab"*)
            printf 'lamboot-repair --fix-fstab --auto --yes%s' "$offline_suffix"
            ;;
        "lamboot-repair --auto"*|"lamboot-repair "*)
            printf 'lamboot-repair --auto --yes%s' "$offline_suffix"
            ;;
        "lamboot-esp clean --apply"*)
            # Skip ESP clean in offline mode — would need live bootloader-active
            # detection we deliberately don't trust from an offline snapshot.
            [[ -n "$LAMBOOT_OFFLINE_DISK" ]] && { printf ''; return; }
            printf 'lamboot-esp clean --apply --yes'
            ;;
        "lamboot-esp clean --dry-run"|"lamboot-esp clean"*)
            [[ -n "$LAMBOOT_OFFLINE_DISK" ]] && { printf ''; return; }
            # Preview-only unless in --auto mode with full ESP
            if [[ $LAMBOOT_AUTO -eq 1 ]] && _esp_is_full; then
                printf 'lamboot-esp clean --apply --yes'
            else
                printf ''
            fi
            ;;
        "lamboot-migrate"*)
            printf ''
            ;;
        "lamboot-esp repair"*)
            printf 'lamboot-repair --auto --yes%s' "$offline_suffix"
            ;;
        "apt-get "*|"dnf "*|"pacman "*|"proxmox-boot-tool "*|"update-grub"|"grub2-mkconfig "*)
            # capcheck-emitted distro commands. Risk-limit gating happens at
            # the policy layer (build_plan_from_findings) via the finding's
            # remediation.risk field — not here. Pass through verbatim; the
            # policy will downgrade to manual if --risk-limit safe is set
            # and the action's risk is moderate/destructive.
            printf '%s' "$cmd"
            ;;
        *)
            printf ''
            ;;
    esac
}

_esp_is_full() {
    local esp
    esp=$(detect_esp 2>/dev/null || true)
    [[ -z "$esp" ]] && return 1
    local avail_kb total_kb
    avail_kb=$(df -k --output=avail "$esp" 2>/dev/null | tail -1 | tr -d ' ' || true)
    total_kb=$(df -k --output=size "$esp" 2>/dev/null | tail -1 | tr -d ' ' || true)
    [[ -z "$avail_kb" ]] && return 1
    [[ -z "$total_kb" ]] && return 1
    [[ "$total_kb" -le 0 ]] && return 1
    local free_pct=$((avail_kb * 100 / total_kb))
    [[ "$free_pct" -lt 10 ]]
}

# ── Show plan ────────────────────────────────────────────────────────────

show_plan() {
    local count=${#PLAN_FINDING_IDS[@]}
    if [[ $count -eq 0 ]]; then
        info "No findings require action."
        return 0
    fi

    printf '\nFound %d issue%s:\n\n' "$count" "$([[ $count -ne 1 ]] && printf 's' || true)" >&2

    local i
    for i in "${!PLAN_FINDING_IDS[@]}"; do
        local sev="${PLAN_SEVERITIES[$i]}"
        local id="${PLAN_FINDING_IDS[$i]}"
        local action="${PLAN_ACTIONS[$i]}"
        local policy="${PLAN_POLICIES[$i]}"

        local sev_color="" marker=""
        case "$sev" in
            critical) sev_color="$LAMBOOT_COLOR_ERROR"; marker="!" ;;
            error)    sev_color="$LAMBOOT_COLOR_ERROR"; marker="X" ;;
            warning)  sev_color="$LAMBOOT_COLOR_WARN";  marker="?" ;;
        esac

        printf '  %s[%s]%s %s\n' "$sev_color" "$sev" "$LAMBOOT_COLOR_RESET" "$id" >&2
        case "$policy" in
            confirm)
                printf '      Action: %s\n      Policy: confirm required (%s severity)\n' \
                    "$action" "$sev" >&2
                ;;
            auto)
                printf '      Action: %s\n      Policy: auto-apply (--auto mode)\n' \
                    "$action" >&2
                ;;
            manual)
                printf '      Suggestion: (run manually) %s\n      Policy: %s — user action\n' \
                    "${action:-see remediation URL}" "$sev" >&2
                ;;
            skip)
                printf '      Skipped by policy or --no-clean/--no-repair.\n' >&2
                ;;
        esac
        printf '\n' >&2
    done
}

# ── Check if plan requires root ──────────────────────────────────────────

plan_needs_root() {
    local i
    for i in "${!PLAN_POLICIES[@]}"; do
        local p="${PLAN_POLICIES[$i]}"
        if [[ "$p" == "auto" ]] || [[ "$p" == "confirm" ]]; then
            return 0
        fi
    done
    return 1
}

# ── Escalation pattern ───────────────────────────────────────────────────

maybe_escalate() {
    [[ $EUID -eq 0 ]] && return 0
    [[ -n "$OPT_RESUME_ID" ]] && return 0  # already resumed
    plan_needs_root || return 0

    info "Plan requires root — re-executing under sudo..."

    [[ -n "$LAMBOOT_RUN_ID" ]] || generate_run_id

    # Save diagnostic output so resumed instance doesn't re-scan
    mkdir -p "$STATE_DIR" 2>/dev/null || {
        # Fallback if we can't create /var/lib/lamboot-doctor/ as non-root
        warn "cannot create $STATE_DIR; re-diagnose will run after sudo"
    }

    # Drop the diagnostic JSON into a tmp file if possible
    # (the sudo process will re-run diagnose if the state file is missing)
    local state_file="$STATE_DIR/${LAMBOOT_RUN_ID}.json"
    if [[ -d "$STATE_DIR" ]] && [[ -w "$STATE_DIR" ]]; then
        printf '%s' "${CACHED_DIAGNOSE_OUTPUT:-}" > "$state_file" 2>/dev/null || true
    fi

    # Re-exec under sudo with --doctor-resume
    exec sudo --preserve-env=LAMBOOT_LIB_DIR "$0" \
        --doctor-resume "$LAMBOOT_RUN_ID" "${ORIGINAL_ARGS[@]}"
}

# ── Execute plan ─────────────────────────────────────────────────────────

execute_plan() {
    local count=${#PLAN_FINDING_IDS[@]}
    [[ $count -eq 0 ]] && return 0

    # Interactive confirmation for the entire plan (unless all-manual)
    local has_executable=0
    for p in "${PLAN_POLICIES[@]}"; do
        [[ "$p" == "auto" ]] || [[ "$p" == "confirm" ]] && has_executable=1
    done

    if [[ $has_executable -eq 1 ]] && [[ $LAMBOOT_AUTO -eq 0 ]] && [[ $LAMBOOT_YES -eq 0 ]]; then
        if ! confirm "Continue with plan" "yes"; then
            record_action "doctor.plan_declined" "plan" "abort" "false" '{}' "null"
            emit_json "$EXIT_ABORT"
            exit "$EXIT_ABORT"
        fi
    fi

    local i
    for i in "${!PLAN_FINDING_IDS[@]}"; do
        local id="${PLAN_FINDING_IDS[$i]}"
        local sev="${PLAN_SEVERITIES[$i]}"
        local action="${PLAN_ACTIONS[$i]}"
        local policy="${PLAN_POLICIES[$i]}"

        case "$policy" in
            manual|skip)
                DOCTOR_SKIPPED=$((DOCTOR_SKIPPED + 1))
                record_action "doctor.step.skipped" "$id" "skipped" "false" \
                    "{\"policy\":\"$policy\",\"severity\":\"$sev\"}" "null"
                continue
                ;;
            confirm)
                # Critical always reconfirms regardless of --auto
                if ! confirm "Apply fix for [$sev] $id" "yes"; then
                    DOCTOR_SKIPPED=$((DOCTOR_SKIPPED + 1))
                    record_action "doctor.step.declined" "$id" "abort" "false" \
                        "{\"severity\":\"$sev\"}" "null"
                    continue
                fi
                ;;
            auto)
                : # proceed without per-step confirm
                ;;
        esac

        info "Applying: $id → $action"

        if [[ $LAMBOOT_DRY_RUN -eq 1 ]]; then
            printf 'DRY-RUN: would exec: %s\n' "$action" >&2
            record_action "doctor.step.applied" "$id" "ok" "true" \
                "{\"action\":\"$(json_escape "$action")\",\"dry_run\":true}" "null"
            DOCTOR_APPLIED=$((DOCTOR_APPLIED + 1))
            continue
        fi

        # Execute via shell — siblings are trusted toolkit binaries
        # shellcheck disable=SC2086  # deliberate word-split to pass flags
        if LAMBOOT_LIB_DIR="${LAMBOOT_LIB_DIR:-}" eval "$action"; then
            success "applied: $id"
            record_action "doctor.step.applied" "$id" "ok" "true" \
                "{\"action\":\"$(json_escape "$action")\",\"severity\":\"$sev\"}" "null"
            DOCTOR_APPLIED=$((DOCTOR_APPLIED + 1))
        else
            warn "failed: $id"
            record_action "doctor.step.failed" "$id" "fail" "false" \
                "{\"action\":\"$(json_escape "$action")\",\"severity\":\"$sev\"}" "null"
            DOCTOR_FAILED=$((DOCTOR_FAILED + 1))
        fi
    done
}

# ── Phase: verify ────────────────────────────────────────────────────────

run_verify() {
    [[ $DOCTOR_APPLIED -eq 0 ]] && return 0

    info "Phase: Verify"

    local diagnose_bin
    diagnose_bin=$(_find_sibling lamboot-diagnose) || return 1

    # Quick re-scan to confirm fixes took effect. Summary counts only.
    local output
    output=$(LAMBOOT_LIB_DIR="${LAMBOOT_LIB_DIR:-}" "$diagnose_bin" --json 2>/dev/null || true)

    if [[ -z "$output" ]]; then
        warn "verification scan produced no output"
        return 0
    fi

    local verify_critical verify_error
    verify_critical=$(printf '%s' "$output" | grep -o '"severity":"critical"' | wc -l | tr -d ' ')
    verify_error=$(printf '%s' "$output" | grep -o '"severity":"error"' | wc -l | tr -d ' ')
    [[ -z "$verify_critical" ]] && verify_critical=0
    [[ -z "$verify_error" ]] && verify_error=0

    emit_finding "doctor.verify.summary" "doctor" "info" "pass" \
        "Post-repair verification: $verify_critical critical, $verify_error error remaining" \
        "If non-zero, re-run doctor or inspect with lamboot-diagnose." \
        "{\"critical\":${verify_critical},\"error\":${verify_error}}" '{}'
}

# ── Show summary ─────────────────────────────────────────────────────────

print_summary() {
    [[ $LAMBOOT_JSON -eq 1 ]] && return 0

    printf '\n%sSummary:%s %d applied, %d manual, %d failed.\n' \
        "$LAMBOOT_COLOR_INFO" "$LAMBOOT_COLOR_RESET" \
        "$DOCTOR_APPLIED" "$DOCTOR_SKIPPED" "$DOCTOR_FAILED" >&2

    # Manual steps: collect and list
    local manual_actions=()
    local i
    for i in "${!PLAN_FINDING_IDS[@]}"; do
        local p="${PLAN_POLICIES[$i]}"
        local a="${PLAN_ACTIONS[$i]}"
        if [[ "$p" == "manual" ]] && [[ -n "$a" ]]; then
            manual_actions+=("sudo $a")
        fi
    done

    if [[ ${#manual_actions[@]} -gt 0 ]]; then
        printf '\nManual steps:\n' >&2
        local mas
        for mas in "${manual_actions[@]}"; do
            printf '  %s\n' "$mas" >&2
        done
        printf '\n' >&2
    fi
}

# ── Cleanup ──────────────────────────────────────────────────────────────

cleanup_resume_state() {
    [[ $OPT_KEEP_RESUME -eq 1 ]] && return 0
    [[ -z "$LAMBOOT_RUN_ID" ]] && return 0
    local state_file="$STATE_DIR/${LAMBOOT_RUN_ID}.json"
    [[ -f "$state_file" ]] && rm -f "$state_file" 2>/dev/null || true
}

# ── Main orchestration ──────────────────────────────────────────────────

do_check() {
    require_siblings

    # Load cached diagnostic output if resuming post-sudo
    local diag_output=""
    if [[ -n "$OPT_RESUME_ID" ]]; then
        LAMBOOT_RUN_ID="$OPT_RESUME_ID"
        local state_file="$STATE_DIR/${OPT_RESUME_ID}.json"
        if [[ -f "$state_file" ]]; then
            diag_output=$(cat "$state_file")
            verbose "loaded cached diagnostic output from $state_file"
        else
            verbose "no cached state found; running diagnose fresh"
        fi
    fi

    if [[ -z "$diag_output" ]]; then
        info "Phase: Diagnose"
        diag_output=$(run_diagnose)
        CACHED_DIAGNOSE_OUTPUT="$diag_output"
    fi

    # Extract findings array from the diagnose output
    local findings_blob
    findings_blob=$(printf '%s' "$diag_output" | grep -oE '"findings":\[.*\],"actions_taken"' | sed 's/"findings":\[//;s/\],"actions_taken"//')

    # Splice in capcheck findings if the bridge is loaded AND capcheck is
    # available. The bridge emits a JSON array; we strip its [ ] wrap and
    # comma-join with the diagnose findings_blob so the existing parser
    # (build_plan_from_findings) treats them uniformly. We also call
    # emit_finding for each capcheck record so it lands in doctor's --json
    # output alongside doctor.* findings.
    if declare -F capcheck_emit_diagnose_findings >/dev/null 2>&1 \
       && capcheck_available \
       && command -v jq >/dev/null 2>&1; then
        info "Phase: Capability audit (lamboot-capcheck)"
        local capcheck_findings
        capcheck_findings=$(capcheck_emit_diagnose_findings)
        local capcheck_count
        capcheck_count=$(printf '%s' "$capcheck_findings" | jq 'length' 2>/dev/null)
        if [[ -n "$capcheck_count" && "$capcheck_count" -gt 0 ]]; then
            # 1) Splice raw into findings_blob for plan-building
            local capcheck_csv
            capcheck_csv=$(printf '%s' "$capcheck_findings" | jq -c '.[]' 2>/dev/null | paste -sd, -)
            if [[ -n "$findings_blob" ]]; then
                findings_blob="${findings_blob},${capcheck_csv}"
            else
                findings_blob="$capcheck_csv"
            fi
            verbose "spliced ${capcheck_count} capcheck finding(s) into plan"

            # 2) Emit each as a doctor-style finding so it appears in
            #    `lamboot-doctor --json` output. Preserve the bridge's
            #    structured remediation (command + risk + summary + doc_url).
            while IFS= read -r row; do
                [[ -z "$row" ]] && continue
                local f_id f_category f_severity f_status f_title f_message
                f_id=$(printf '%s' "$row" | jq -r '.id')
                f_category=$(printf '%s' "$row" | jq -r '.category')
                f_severity=$(printf '%s' "$row" | jq -r '.severity')
                f_status=$(printf '%s' "$row" | jq -r '.status')
                f_title=$(printf '%s' "$row" | jq -r '.title')
                f_message=$(printf '%s' "$row" | jq -r '.description')
                local remediation
                remediation=$(printf '%s' "$row" | jq -c '.remediation')
                local context='{}'
                emit_finding "$f_id" "$f_category" "$f_severity" "$f_status" \
                    "$f_title" "$f_message" "$context" "$remediation"
            done < <(printf '%s' "$capcheck_findings" | jq -c '.[]')
        fi
    fi

    build_plan_from_findings "$findings_blob"

    info "Phase: Plan"
    show_plan

    local plan_count=${#PLAN_FINDING_IDS[@]}
    if [[ $plan_count -eq 0 ]]; then
        info "System is healthy — nothing to do."
        emit_finding "doctor.check.healthy" "doctor" "info" "pass" \
            "No issues requiring action" "" "{}" '{}'
        emit_json "$EXIT_NOOP"
        exit "$EXIT_NOOP"
    fi

    # Emit a summary finding about the plan
    emit_finding "doctor.plan.summary" "doctor" "info" "pass" \
        "Built plan for $plan_count issue(s)" "" \
        "{\"total\":${plan_count}}" '{}'

    # Escalate if needed
    maybe_escalate

    # Execute
    info "Phase: Execute"
    execute_plan

    # Verify
    run_verify

    # Summary
    print_summary

    # Cleanup
    cleanup_resume_state

    local exit_code="$EXIT_OK"
    if [[ $DOCTOR_FAILED -gt 0 ]] && [[ $DOCTOR_APPLIED -gt 0 ]]; then
        exit_code="$EXIT_PARTIAL"
    elif [[ $DOCTOR_FAILED -gt 0 ]]; then
        exit_code="$EXIT_ERROR"
    fi

    emit_json "$exit_code"
    exit "$exit_code"
}

# ── CLI ──────────────────────────────────────────────────────────────────

ORIGINAL_ARGS=("$@")
CACHED_DIAGNOSE_OUTPUT=""

# ── Guided hygiene: clean / restore (backup + reversible + logged) ────────────

_doctor_clean_log() {  # msg [backup_dir]
    local line; line="$(date -u +%Y-%m-%dT%H:%M:%SZ) lamboot-doctor: $1"
    mkdir -p "$(dirname "$DOCTOR_CLEAN_LOG")" 2>/dev/null \
        && printf '%s\n' "$line" >> "$DOCTOR_CLEAN_LOG" 2>/dev/null || true
    if [[ -n "${2:-}" ]]; then
        mkdir -p "$2" 2>/dev/null
        printf '%s\n' "$line" >> "$2/actions.log" 2>/dev/null || true
    fi
}

# Surfaces default to esp + nvram (NOT boot — kernel removal is PM-deferential).
_surface_enabled() {
    if [[ -z "$OPT_ONLY" ]]; then
        case "$1" in esp|nvram) return 0 ;; *) return 1 ;; esac
    fi
    printf '%s' "$OPT_ONLY" | tr ',' '\n' | grep -qx "$1"
}

_esp_clean_candidate_files() {  # esp_bin -> newline-separated file list
    "$1" clean --json 2>/dev/null | python3 -c '
import sys, json
try: d = json.load(sys.stdin)
except Exception: sys.exit(0)
for f in d.get("findings", []):
    for x in ((f.get("context") or {}).get("files") or []):
        print(x)
' 2>/dev/null || true
}

do_clean() {
    [[ $OPT_CLEAN_APPLY -eq 1 ]] && require_root
    local backup_dir="${OPT_BACKUP_DIR:-/var/backups/lamboot-doctor-$(date -u +%Y-%m-%dT%H-%M-%S)}"
    local nvram_done=0 esp_backed=0

    info "lamboot-doctor clean (apply=${OPT_CLEAN_APPLY}, surfaces=${OPT_ONLY:-esp,nvram}, backup=${backup_dir})"
    if [[ $OPT_CLEAN_APPLY -eq 1 ]]; then
        if ! confirm "CLEAN ESP/NVRAM (backed up + reversible)" "yes"; then
            emit_json "$EXIT_ABORT"; exit "$EXIT_ABORT"
        fi
        mkdir -p "$backup_dir"
        _doctor_clean_log "clean --apply surfaces=${OPT_ONLY:-esp,nvram} backup=$backup_dir" "$backup_dir"
    fi

    if _surface_enabled nvram; then
        local nv
        if nv=$(_find_sibling lamboot-nvram); then
            local args=(clean)
            [[ -n "$OPT_CLEAN_CATEGORY" ]] && args+=(--category "$OPT_CLEAN_CATEGORY")
            if [[ $OPT_CLEAN_APPLY -eq 1 ]]; then
                args+=(--apply --backup-dir "$backup_dir/nvram")
                "$nv" "${args[@]}" >&2 && nvram_done=1 || true
                _doctor_clean_log "ran lamboot-nvram clean --apply" "$backup_dir"
            else
                "$nv" "${args[@]}" >&2 || true
            fi
        else
            warn "lamboot-nvram not found; skipping NVRAM surface"
        fi
    fi

    if _surface_enabled esp; then
        local esp
        if esp=$(_find_sibling lamboot-esp); then
            local files; files=$(_esp_clean_candidate_files "$esp")
            local n; n=$(printf '%s' "$files" | grep -c . 2>/dev/null || echo 0)
            if [[ $OPT_CLEAN_APPLY -eq 1 && -n "$files" ]]; then
                while IFS= read -r f; do
                    [[ -z "$f" || ! -e "$f" ]] && continue
                    mkdir -p "$backup_dir/esp$(dirname "$f")" 2>/dev/null
                    cp -a "$f" "$backup_dir/esp$f" 2>/dev/null \
                        && { esp_backed=$((esp_backed + 1)); _doctor_clean_log "backed up $f" "$backup_dir"; }
                done <<< "$files"
                "$esp" clean --apply --yes >&2 || warn "lamboot-esp clean reported issues"
                _doctor_clean_log "ran lamboot-esp clean --apply ($esp_backed files backed up first)" "$backup_dir"
            else
                info "ESP: ${n} stale file(s) would be removed (preview)"
            fi
        else
            warn "lamboot-esp not found; skipping ESP surface"
        fi
    fi

    if _surface_enabled boot; then
        info "/boot: kernel hygiene defers to the package manager — review old kernels with 'apt autoremove' / 'dnf remove' (never auto-removed here)."
    fi

    if [[ $OPT_CLEAN_APPLY -eq 1 ]]; then
        printf '{"backup_dir":"%s","surfaces":"%s","nvram_cleaned":%d,"esp_files_backed_up":%d}\n' \
            "$backup_dir" "${OPT_ONLY:-esp,nvram}" "$nvram_done" "$esp_backed" > "$backup_dir/MANIFEST.json"
        success "Clean complete. Backup + log: $backup_dir"
        info "Reverse with: sudo lamboot-doctor restore --backup-dir $backup_dir"
    else
        info "Dry-run preview complete. Re-run with --apply (backed up + reversible + logged)."
    fi
    emit_json "$EXIT_OK"; exit "$EXIT_OK"
}

do_restore() {
    local backup_dir="${OPT_BACKUP_DIR:-}"
    [[ -z "$backup_dir" ]] && backup_dir=$(ls -td /var/backups/lamboot-doctor-* 2>/dev/null | head -1)
    [[ -n "$backup_dir" && -f "$backup_dir/MANIFEST.json" ]] \
        || die_prerequisite "no lamboot-doctor backup found (looked in ${backup_dir:-/var/backups/lamboot-doctor-*})"
    require_root

    info "lamboot-doctor restore from $backup_dir"
    _doctor_clean_log "restore from $backup_dir" "$backup_dir"

    if _surface_enabled esp && [[ -d "$backup_dir/esp" ]]; then
        local esp_n=0
        while IFS= read -r f; do
            local target="${f#"$backup_dir"/esp}"
            mkdir -p "$(dirname "$target")" 2>/dev/null
            cp -a "$f" "$target" 2>/dev/null && { esp_n=$((esp_n + 1)); _doctor_clean_log "restored $target" "$backup_dir"; }
        done < <(find "$backup_dir/esp" -type f 2>/dev/null)
        info "ESP: restored $esp_n file(s)"
    fi

    if _surface_enabled nvram && [[ -d "$backup_dir/nvram" ]]; then
        local nv
        if nv=$(_find_sibling lamboot-nvram); then
            "$nv" restore --apply --backup-dir "$backup_dir/nvram" >&2 || warn "lamboot-nvram restore reported issues"
            _doctor_clean_log "ran lamboot-nvram restore --apply" "$backup_dir"
        fi
    fi

    success "Restore complete from $backup_dir"
    emit_json "$EXIT_OK"; exit "$EXIT_OK"
}

parse_args() {
    while [[ $# -gt 0 ]]; do
        parse_common_flag "$1"
        local rc=$?
        case $rc in
            1) shift; continue ;;
            2) _register_subcommands; dispatch_help; exit 0 ;;
            3) printf 'lamboot-doctor %s (lamboot-tools %s)\n' \
                "$LAMBOOT_TOOL_VERSION" "$LAMBOOT_TOOLKIT_VERSION"; exit 0 ;;
            4) printf '{"$schema":"https://github.com/lamco-admin/lamboot-tools"}\n'; exit 0 ;;
            5) LAMBOOT_OFFLINE_DISK="${2:-}"; [[ -n "$LAMBOOT_OFFLINE_DISK" ]] || die "--offline requires DISK argument"; shift 2; continue ;;
            0) ;;
        esac

        case "$1" in
            --no-repair)          OPT_NO_REPAIR=1; shift ;;
            --no-clean)           OPT_NO_CLEAN=1; shift ;;
            --risk-limit)         OPT_RISK_LIMIT="${2:-moderate}"; shift 2 ;;
            --doctor-resume)      OPT_RESUME_ID="${2:-}"; shift 2 ;;
            --keep-resume-state)  OPT_KEEP_RESUME=1; shift ;;
            --apply)              OPT_CLEAN_APPLY=1; shift ;;
            --only)               OPT_ONLY="${2:-}"; shift 2 ;;
            --category)           OPT_CLEAN_CATEGORY="${2:-}"; shift 2 ;;
            --backup-dir)         OPT_BACKUP_DIR="${2:-}"; shift 2 ;;
            clean|restore)        OPT_SUBCOMMAND="$1"; shift ;;
            help)                 _register_subcommands
                                  if [[ -n "${2:-}" ]]; then
                                      dispatch_help "$2"
                                  else
                                      dispatch_help
                                  fi
                                  exit 0 ;;
            check)                shift ;;
            -*)                   die "unknown option: $1" ;;
            *)                    die "unknown argument: $1" ;;
        esac
    done
}

main() {
    parse_args "$@"
    case "$OPT_SUBCOMMAND" in
        clean)   do_clean ;;
        restore) do_restore ;;
        *)       do_check ;;
    esac
}

main "$@"
