import path from "node:path";
import { normalizeMcpPack } from "./recommendations.js";
import { ensureDir, pathExists, readJson, writeText } from "../utils/fs.js";
export function getMemoryPaths(targetRoot) {
    const root = path.join(targetRoot, ".agent", "memory");
    return {
        root,
        repoFacts: path.join(root, "repo-facts.json"),
        architectureDecisions: path.join(root, "architecture-decisions.md"),
        domainGlossary: path.join(root, "domain-glossary.md"),
        currentFocus: path.join(root, "current-focus.json"),
        openRisks: path.join(root, "open-risks.json"),
        knownGotchas: path.join(root, "known-gotchas.md"),
        lastSuccessfulPatterns: path.join(root, "last-successful-patterns.md")
    };
}
export async function ensureMemoryLayout(targetRoot) {
    const paths = getMemoryPaths(targetRoot);
    await ensureDir(paths.root);
    return paths;
}
export async function loadCurrentFocus(targetRoot) {
    const paths = getMemoryPaths(targetRoot);
    if (!(await pathExists(paths.currentFocus))) {
        return null;
    }
    return readJson(paths.currentFocus);
}
export async function loadOpenRisks(targetRoot) {
    const paths = getMemoryPaths(targetRoot);
    if (!(await pathExists(paths.openRisks))) {
        return null;
    }
    return readJson(paths.openRisks);
}
export async function syncCurrentFocusFromActiveHints(targetRoot, hints) {
    const paths = await ensureMemoryLayout(targetRoot);
    const current = (await loadCurrentFocus(targetRoot)) ?? {
        artifactType: "shrey-junior/current-focus",
        version: 1,
        updatedAt: new Date().toISOString(),
        currentFocus: hints.nextAction,
        focusOwnerRole: hints.activeRole,
        nextRecommendedSpecialist: hints.nextRecommendedSpecialist,
        nextSuggestedMcpPack: normalizeMcpPack(hints.nextSuggestedMcpPack),
        nextFileToRead: hints.nextFileToRead,
        nextSuggestedCommand: hints.nextSuggestedCommand,
        latestCheckpoint: hints.latestCheckpoint,
        latestTaskPacket: hints.latestTaskPacket,
        latestHandoff: hints.latestHandoff,
        latestDispatchManifest: hints.latestDispatchManifest,
        latestReconcile: hints.latestReconcile
    };
    const next = {
        ...current,
        updatedAt: new Date().toISOString(),
        currentFocus: hints.nextAction || current.currentFocus,
        focusOwnerRole: hints.activeRole,
        nextRecommendedSpecialist: hints.nextRecommendedSpecialist || current.nextRecommendedSpecialist,
        nextSuggestedMcpPack: normalizeMcpPack(hints.nextSuggestedMcpPack || current.nextSuggestedMcpPack),
        nextFileToRead: hints.nextFileToRead || current.nextFileToRead,
        nextSuggestedCommand: hints.nextSuggestedCommand || current.nextSuggestedCommand,
        latestCheckpoint: hints.latestCheckpoint ?? current.latestCheckpoint,
        latestTaskPacket: hints.latestTaskPacket ?? current.latestTaskPacket,
        latestHandoff: hints.latestHandoff ?? current.latestHandoff,
        latestDispatchManifest: hints.latestDispatchManifest ?? current.latestDispatchManifest,
        latestReconcile: hints.latestReconcile ?? current.latestReconcile
    };
    await writeText(paths.currentFocus, `${JSON.stringify(next, null, 2)}\n`);
    return paths.currentFocus;
}
export async function writeOpenRisksRecord(targetRoot, source, risks) {
    const paths = await ensureMemoryLayout(targetRoot);
    const next = {
        artifactType: "shrey-junior/open-risks",
        version: 1,
        updatedAt: new Date().toISOString(),
        source,
        risks: [...new Set(risks.filter((risk) => Boolean(risk.trim())))]
    };
    await writeText(paths.openRisks, `${JSON.stringify(next, null, 2)}\n`);
    return paths.openRisks;
}
