/**
 * Workspace identity + planning store facts for `dashboard-summary.systemStatus`.
 */
import fs from "node:fs/promises";
import path from "node:path";
import { planningSqliteDatabaseRelativePath } from "../planning-config.js";
export async function buildDashboardWorkspaceIdentity(workspacePath) {
    const projectContextPath = path.join(workspacePath, ".workspace-kit", "generated", "project-context.json");
    let projectName = null;
    try {
        const raw = await fs.readFile(projectContextPath, "utf8");
        const j = JSON.parse(raw);
        if (typeof j.projectName === "string" && j.projectName.trim().length > 0) {
            projectName = j.projectName.trim();
        }
    }
    catch {
        /* optional generated file */
    }
    const rootPkgPath = path.join(workspacePath, "package.json");
    let packageName = null;
    let rootPackageVersion = null;
    try {
        const raw = await fs.readFile(rootPkgPath, "utf8");
        const j = JSON.parse(raw);
        if (typeof j.name === "string" && j.name.trim().length > 0) {
            packageName = j.name.trim();
        }
        if (typeof j.version === "string" && j.version.trim().length > 0) {
            rootPackageVersion = j.version.trim();
        }
    }
    catch {
        /* missing or invalid */
    }
    const wkPkgPath = path.join(workspacePath, "node_modules", "@workflow-cannon", "workspace-kit", "package.json");
    let workspaceKitVersion = null;
    try {
        const raw = await fs.readFile(wkPkgPath, "utf8");
        const j = JSON.parse(raw);
        if (typeof j.version === "string" && j.version.trim().length > 0) {
            workspaceKitVersion = j.version.trim();
        }
    }
    catch {
        /* dev checkout without npm install, etc. */
    }
    return {
        schemaVersion: 1,
        projectName,
        packageName,
        workspaceKitVersion,
        rootPackageVersion
    };
}
export function buildDashboardPlanningStoreSummary(ctx) {
    return {
        schemaVersion: 1,
        backend: "sqlite",
        databaseRelativePath: planningSqliteDatabaseRelativePath(ctx)
    };
}
