/**
 * `neotoma packs` subcommand: list, install, uninstall, and inspect status of
 * vertical packs.
 */
import { installVerticalPack, uninstallVerticalPack, getInstalledPacks, isVerticalPackInstalled, listPacksWithStatus, readManifest, } from "../services/verticals/install.js";
import { resolveVerticalPack, VERTICAL_PACKS, } from "../services/verticals/registry.js";
function describePack(pack, installed, installed_at) {
    return {
        slug: pack.slug,
        label: pack.label,
        installed,
        installed_at,
        has_schema_manifest: Boolean(pack.schema_manifest_path),
        has_sandbox_manifest: Boolean(pack.sandbox_manifest_path),
        meta_fallback: pack.meta_fallback,
        entity_examples: [...pack.entityExamples],
    };
}
export async function runPacksList(options = {}) {
    const mode = options.mode ?? "text";
    const status = listPacksWithStatus();
    const rows = status.map((entry) => describePack(entry.pack, entry.installed, entry.installed_at));
    if (mode === "json") {
        process.stdout.write(JSON.stringify({ packs: rows }, null, 2) + "\n");
        return;
    }
    process.stdout.write(`Available vertical packs (${rows.length}):\n\n`);
    for (const row of rows) {
        const flags = [];
        if (row.installed)
            flags.push("installed");
        if (row.has_schema_manifest)
            flags.push("schema");
        if (row.has_sandbox_manifest)
            flags.push("sandbox");
        if (row.meta_fallback)
            flags.push("meta-fallback");
        const flagStr = flags.length ? `  [${flags.join(", ")}]` : "";
        process.stdout.write(`  - ${row.slug.padEnd(22)} ${row.label}${flagStr}\n`);
    }
    process.stdout.write("\n");
    process.stdout.write("Run 'neotoma packs install <slug>' to install a pack's schemas.\n");
}
export async function runPacksInstall(slug, options = {}) {
    const mode = options.mode ?? "text";
    const pack = resolveVerticalPack(slug);
    if (!pack.schema_manifest_path) {
        if (mode === "json") {
            process.stdout.write(JSON.stringify({ ok: true, slug: pack.slug, note: "no schema manifest; nothing to install" }, null, 2) + "\n");
        }
        else {
            process.stdout.write(`Pack '${pack.slug}' has no schema manifest yet; nothing to install.\n`);
        }
        return;
    }
    const result = await installVerticalPack(pack.slug, {
        user_id: options.userId,
    });
    if (mode === "json") {
        process.stdout.write(JSON.stringify(result, null, 2) + "\n");
        return;
    }
    process.stdout.write(`Installed pack '${pack.slug}'\n`);
    if (result.installed.length) {
        process.stdout.write(`  Newly registered: ${result.installed.map((s) => s.entity_type).join(", ")}\n`);
    }
    if (result.upgraded.length) {
        process.stdout.write(`  Upgraded:          ${result.upgraded.map((s) => s.entity_type).join(", ")}\n`);
    }
    if (result.skipped.length) {
        process.stdout.write(`  Skipped:\n`);
        for (const item of result.skipped) {
            process.stdout.write(`    - ${item.entity_type}: ${item.reason}\n`);
        }
    }
}
export async function runPacksUninstall(slug, options = {}) {
    const mode = options.mode ?? "text";
    const result = await uninstallVerticalPack(slug);
    if (mode === "json") {
        process.stdout.write(JSON.stringify(result, null, 2) + "\n");
        return;
    }
    if (!result.removed_record) {
        process.stdout.write(`Pack '${slug}' is not currently installed.\n`);
        return;
    }
    process.stdout.write(`Removed install record for '${result.slug}'. Schemas retained: ${result.retained_schemas.join(", ")}\n`);
}
export async function runPacksStatus(slug, options = {}) {
    const mode = options.mode ?? "text";
    if (slug) {
        const pack = resolveVerticalPack(slug);
        const installed = isVerticalPackInstalled(pack.slug);
        const records = getInstalledPacks();
        const record = records.find((r) => r.slug === pack.slug);
        let manifestPreview = [];
        if (pack.schema_manifest_path) {
            try {
                const manifest = readManifest(pack);
                manifestPreview = manifest.schemas.map((s) => ({
                    entity_type: s.entity_type,
                    schema_version: s.schema_version,
                }));
            }
            catch {
                // ignore — surfaced separately if needed
            }
        }
        const payload = {
            slug: pack.slug,
            label: pack.label,
            installed,
            installed_at: record?.installed_at,
            manifest_schemas: manifestPreview,
            installed_schemas: record?.schemas ?? [],
        };
        if (mode === "json") {
            process.stdout.write(JSON.stringify(payload, null, 2) + "\n");
            return;
        }
        process.stdout.write(`Pack: ${pack.slug} (${pack.label})\n`);
        process.stdout.write(`  Installed: ${installed ? "yes" : "no"}\n`);
        if (record?.installed_at) {
            process.stdout.write(`  Installed at: ${record.installed_at}\n`);
        }
        if (manifestPreview.length) {
            process.stdout.write(`  Manifest declares:\n`);
            for (const s of manifestPreview) {
                process.stdout.write(`    - ${s.entity_type}@${s.schema_version}\n`);
            }
        }
        return;
    }
    const installed = getInstalledPacks();
    const known = new Set(VERTICAL_PACKS.map((p) => p.slug));
    if (mode === "json") {
        process.stdout.write(JSON.stringify({
            installed_count: installed.length,
            installed,
            unknown: installed
                .filter((r) => !known.has(r.slug))
                .map((r) => r.slug),
        }, null, 2) + "\n");
        return;
    }
    if (!installed.length) {
        process.stdout.write("No vertical packs are currently installed.\n");
        return;
    }
    process.stdout.write(`Installed packs (${installed.length}):\n`);
    for (const record of installed) {
        process.stdout.write(`  - ${record.slug} (${record.schemas.length} schemas) — ${record.installed_at}\n`);
    }
}
//# sourceMappingURL=packs.js.map