/**
 * `neotoma setup --activate` — context detection for activation.
 *
 * Promoted from install.md Phase 3 Step 0. Before preference selection,
 * detect whether the install directory is developer-oriented, personal,
 * or mixed, so the agent doesn't default to personal-file ingestion in
 * a directory full of git repos.
 *
 * Returns a structured report. The CLI handler renders it as JSON for agents
 * or a human-readable summary for interactive use, then asks the user to
 * confirm/override the detected context.
 */
import { existsSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";
/**
 * Folder name patterns that lightly indicate a personal-data directory.
 * Conservative list — false positives are worse than false negatives here
 * because the agent always asks the user to confirm.
 */
const PERSONAL_SIGNAL_FOLDERS = [
    "notes",
    "contracts",
    "invoices",
    "transcripts",
    "financial",
    "clients",
    "documents",
    "journal",
    "writing",
    "personal",
    "household",
];
function isDirectory(p) {
    try {
        return statSync(p).isDirectory();
    }
    catch {
        return false;
    }
}
function listSubdirs(cwd) {
    try {
        return readdirSync(cwd, { withFileTypes: true })
            .filter((d) => d.isDirectory() && !d.name.startsWith("."))
            .map((d) => d.name);
    }
    catch {
        return [];
    }
}
/**
 * Scan the given directory and return the structured context report.
 * Pure function (modulo filesystem reads); no prompts, no writes.
 */
export function detectActivationContext(cwd) {
    const cwdIsGitRepo = isDirectory(join(cwd, ".git"));
    const subs = listSubdirs(cwd);
    const gitRepos = [];
    const personalSignals = [];
    for (const name of subs) {
        const sub = join(cwd, name);
        if (isDirectory(join(sub, ".git"))) {
            gitRepos.push(name);
            continue;
        }
        if (PERSONAL_SIGNAL_FOLDERS.includes(name.toLowerCase())) {
            personalSignals.push(name);
        }
    }
    // Heuristic for the suggested context. The user always confirms.
    // - Multiple git repos (2+) and no personal signals → developer.
    // - One git repo + personal signals OR cwd_is_git_repo with no subs → mixed.
    // - No git repos, has personal signals → personal.
    // - Otherwise unknown (let the user decide).
    let detected;
    if (gitRepos.length >= 2 && personalSignals.length === 0) {
        detected = "developer";
    }
    else if (gitRepos.length >= 1 && personalSignals.length >= 1) {
        detected = "mixed";
    }
    else if (gitRepos.length >= 2 && personalSignals.length >= 1) {
        detected = "mixed";
    }
    else if (gitRepos.length === 0 && personalSignals.length >= 1) {
        detected = "personal";
    }
    else if (gitRepos.length === 1 && personalSignals.length === 0) {
        // A single repo at the top level usually means the user is inside the repo
        // (e.g. `neotoma setup --activate` run from a checkout), not a developer
        // projects directory of many repos.
        detected = cwdIsGitRepo ? "developer" : "unknown";
    }
    else if (cwdIsGitRepo) {
        detected = "developer";
    }
    else {
        detected = "unknown";
    }
    const repoSummary = gitRepos.length > 0
        ? `${gitRepos.length} git ${gitRepos.length === 1 ? "repository" : "repositories"}: ${gitRepos.slice(0, 5).join(", ")}${gitRepos.length > 5 ? ", ..." : ""}`
        : cwdIsGitRepo
            ? "this directory is itself a git repository"
            : "no git repositories";
    const personalSummary = personalSignals.length > 0
        ? `; personal-file signals: ${personalSignals.join(", ")}`
        : "";
    const summaryLine = `I see ${repoSummary}${personalSummary}.`;
    const followupQuestion = [
        "Is this primarily:",
        "(a) a developer/projects directory (repos for code projects),",
        "(b) a personal-data directory (notes, clients, documents),",
        "(c) a mix?",
    ].join("\n");
    return {
        cwd,
        git_repos: gitRepos,
        cwd_is_git_repo: cwdIsGitRepo,
        personal_signals: personalSignals,
        detected_context: detected,
        summary_line: summaryLine,
        followup_question: followupQuestion,
    };
}
/**
 * Run the activation context-detection step. Pure: no prompts, no writes.
 * The CLI handler renders the report and asks the user the followup question
 * when `next_branch === "ask_user"`.
 */
export function runActivate(options = {}) {
    const cwd = options.cwd ?? process.cwd();
    if (!existsSync(cwd)) {
        throw new Error(`runActivate: cwd does not exist: ${cwd}`);
    }
    const scan = detectActivationContext(cwd);
    const confirmed = options.context ?? null;
    const effective = confirmed ?? scan.detected_context;
    let nextBranch;
    switch (effective) {
        case "personal":
            nextBranch = "personal_data_ingest";
            break;
        case "developer":
            nextBranch = "developer_repo_integration";
            break;
        case "mixed":
            nextBranch = "mixed_developer_then_personal";
            break;
        case "unknown":
        default:
            nextBranch = confirmed ? null : "ask_user";
            break;
    }
    return {
        ...scan,
        confirmed_context: confirmed,
        next_branch: nextBranch,
    };
}
//# sourceMappingURL=activate.js.map