/*
 * Runtime storage plane / initialization plan.
 * Purpose: describe and optionally apply directory creation without storing secrets.
 */
import { mkdir } from "node:fs/promises";
import { existsSync, statSync } from "node:fs";
function existsDirectory(input) {
    try {
        return existsSync(input) && statSync(input).isDirectory();
    }
    catch {
        return false;
    }
}
function unique(values) {
    return [...new Set(values)];
}
export function planRaxStorageInit(layout) {
    const homeDirectories = [
        layout.home.root,
        layout.home.config,
        layout.home.authRefs,
        layout.home.providerProfiles,
        layout.home.packageCache,
        layout.home.toolDeps,
        layout.home.trust,
        layout.home.logs,
        layout.home.runtimeSockets,
        layout.home.templates,
    ];
    const workspaceDirectories = [
        layout.workspace.root,
        layout.workspace.config,
        layout.workspace.manifests,
        layout.workspace.sessions,
        layout.workspace.state,
        layout.workspace.events,
        layout.workspace.approvals,
        layout.workspace.artifacts,
        layout.workspace.cache,
        layout.workspace.sandbox,
        layout.workspace.reports,
        layout.workspace.agentsRoot,
    ];
    const agentDirectories = layout.workspace.agent === undefined
        ? []
        : [
            layout.workspace.agent.root,
            layout.workspace.agent.manifests,
            layout.workspace.agent.sessions,
            layout.workspace.agent.state,
            layout.workspace.agent.events,
            layout.workspace.agent.approvals,
            layout.workspace.agent.artifacts,
            layout.workspace.agent.cache,
            layout.workspace.agent.reports,
        ];
    return {
        kind: "praxis.raxStorageInitPlan",
        layoutProtocol: layout.protocolVersion,
        directories: [
            ...unique(homeDirectories).map((directory) => ({
                path: directory,
                scope: "home",
                existing: existsDirectory(directory),
            })),
            ...unique(workspaceDirectories).map((directory) => ({
                path: directory,
                scope: "workspace",
                existing: existsDirectory(directory),
            })),
            ...unique(agentDirectories).map((directory) => ({
                path: directory,
                scope: "agent",
                existing: existsDirectory(directory),
            })),
        ],
        files: [],
        createsDirectories: true,
        createsFiles: false,
        writesSecrets: false,
        gitignoreRecommendation: [".rax_workspace/"],
    };
}
export async function applyRaxStorageInitPlan(plan) {
    const created = [];
    const skipped = [];
    for (const directory of plan.directories) {
        if (directory.existing || existsDirectory(directory.path)) {
            skipped.push(directory.path);
            continue;
        }
        await mkdir(directory.path, { recursive: true });
        created.push(directory.path);
    }
    return {
        ok: true,
        createdDirectories: created,
        skippedDirectories: skipped,
        events: ["runtime.storagePlane.init.applied"],
    };
}
