/**
 * Stage 0.5 orchestrator: combines vertical detection + user_preference helpers
 * into a single decision payload that the agent activation flow can act on.
 *
 * The orchestrator is pure: callers supply already-scanned filesystem entries
 * (FileSignal[]), and this module returns
 *   - the recommended next action (auto-confirm / ask-disambiguation / ask-default-fallback / skipped)
 *   - the operator-facing copy the agent should display
 *   - a ready-to-store user_preference payload for the chosen path
 *
 * Filesystem IO and actual `store_structured` calls live in the agent-side
 * runner (CLI, MCP server, or hook). This separation keeps Stage 0.5 testable
 * end-to-end without spinning up SQLite or the MCP transport.
 */
import { detectVerticalFromSignals, } from "./vertical_detection.js";
import { buildUserPreferenceUpsert, } from "./user_preference.js";
import { findVerticalPack } from "../verticals/registry.js";
const FALLBACK_SLUG = "personal-agent";
/**
 * Run Stage 0.5 of the activation flow against an already-scanned directory.
 * Does NOT perform IO. Caller decides whether to actually display copy and
 * persist the resulting payload.
 */
export function runStageZeroFive(input) {
    const recordedAt = new Date().toISOString();
    if (!input.permissionGranted) {
        return {
            action: "skipped",
            detection: null,
            recommendedVertical: null,
            ambiguousCandidates: [],
            promptCopy: "Skipping vertical detection — Neotoma will continue with generic onboarding. You can run `neotoma packs install <slug>` later if you want to seed a use-case pack.",
            userPreference: buildUserPreferenceUpsert({
                onboarding_mode: input.onboardingMode,
                preference_categories: input.preferenceCategories,
                canonical_name: input.canonicalName,
                notes: "Stage 0.5 vertical detection skipped (permission declined).",
            }),
            packInstallCommand: null,
        };
    }
    const detection = detectVerticalFromSignals(input.entries, input.detectionOptions);
    if (!detection.topVertical) {
        return {
            action: "fallback_personal_agent",
            detection,
            recommendedVertical: FALLBACK_SLUG,
            ambiguousCandidates: [],
            promptCopy: "I did not see strong signals for a specific use case here. I'll default to the `personal-agent` meta-fallback (no native schemas) and you can switch later with `neotoma packs install <slug>`.",
            userPreference: buildUserPreferenceUpsert({
                primary_vertical: FALLBACK_SLUG,
                onboarding_mode: input.onboardingMode,
                preference_categories: input.preferenceCategories,
                canonical_name: input.canonicalName,
                vertical_detection: buildDetectionRecord(detection, "filesystem_signals", recordedAt),
                notes: "Stage 0.5 detected no strong vertical signal; fell back to personal-agent.",
            }),
            packInstallCommand: null,
        };
    }
    if (detection.ambiguous) {
        const candidates = detection.scores.slice(0, 4).map((s) => s.slug);
        return {
            action: "ask_disambiguation",
            detection,
            recommendedVertical: detection.topVertical,
            ambiguousCandidates: candidates,
            promptCopy: buildAmbiguityCopy(candidates),
            userPreference: buildUserPreferenceUpsert({
                onboarding_mode: input.onboardingMode,
                preference_categories: input.preferenceCategories,
                canonical_name: input.canonicalName,
                vertical_detection: buildDetectionRecord(detection, "filesystem_signals", recordedAt),
                notes: "Stage 0.5 produced ambiguous scoring; awaiting user disambiguation.",
            }),
            packInstallCommand: null,
        };
    }
    const slug = detection.topVertical;
    const pack = findVerticalPack(slug);
    return {
        action: "auto_confirm",
        detection,
        recommendedVertical: slug,
        ambiguousCandidates: [],
        promptCopy: buildAutoConfirmCopy(slug, pack?.label ?? slug),
        userPreference: buildUserPreferenceUpsert({
            primary_vertical: slug,
            onboarding_mode: input.onboardingMode,
            preference_categories: input.preferenceCategories,
            canonical_name: input.canonicalName,
            vertical_detection: buildDetectionRecord(detection, "filesystem_signals", recordedAt),
        }),
        packInstallCommand: `neotoma packs install ${slug}`,
    };
}
function buildDetectionRecord(detection, method, recordedAt) {
    const scores = {};
    for (const s of detection.scores)
        scores[s.slug] = s.score;
    return {
        method,
        ambiguous: detection.ambiguous,
        scores,
        recorded_at: recordedAt,
    };
}
function buildAutoConfirmCopy(slug, label) {
    return [
        `This looks like a **${label}** workflow.`,
        `Want me to seed the matching record types pack?`,
        `Run \`neotoma packs install ${slug}\` (or say \"yes\" and I'll do it for you).`,
    ].join(" ");
}
function buildAmbiguityCopy(candidates) {
    if (candidates.length === 0) {
        return "I see signals across several use cases here — which is the primary one for this directory?";
    }
    const list = candidates.map((c) => `\`${c}\``).join(", ");
    return [
        `I see signals for multiple use cases here (${list}).`,
        `Which one is the primary workflow for this directory?`,
        `Pick \`personal-agent\` if this is your general operator setup.`,
    ].join(" ");
}
//# sourceMappingURL=stage_zero_five.js.map