import { mkdir, stat, writeFile } from "fs/promises";
import { join } from "path";
import { getRunPath, pathExists } from "../util/fs.js";
import { runShell } from "../util/shell.js";
export function getCompletionArtifactPaths(root, runId) {
    const runDir = getRunPath(runId, undefined, root);
    const artifactsDir = join(runDir, "artifacts");
    return {
        runDir,
        artifactsDir,
        contractJson: join(runDir, "completion-contract.json"),
        resultJson: join(runDir, "result.json"),
        verifyJson: join(runDir, "verify-result.json"),
        evidenceJson: join(runDir, "evidence.json"),
        diffPatch: join(artifactsDir, "generated-diff.patch"),
        diffStat: join(artifactsDir, "generated-diff.stat.txt"),
        testLog: join(artifactsDir, "test.log"),
        runLog: join(runDir, "events.jsonl"),
    };
}
export async function ensureCompletionArtifactContract(root, runId) {
    const paths = getCompletionArtifactPaths(root, runId);
    await mkdir(paths.artifactsDir, { recursive: true });
    const contract = {
        schemaVersion: 1,
        runId,
        required: {
            diffPatch: paths.diffPatch,
            testLog: paths.testLog,
            runLog: paths.runLog,
            resultJson: paths.resultJson,
        },
        generated: {
            verifyJson: paths.verifyJson,
            evidenceJson: paths.evidenceJson,
            diffStat: paths.diffStat,
        },
    };
    await writeFile(paths.contractJson, `${JSON.stringify(contract, null, 2)}\n`, "utf-8");
    return paths;
}
export async function captureGitDiffArtifacts(root, runId) {
    const paths = await ensureCompletionArtifactContract(root, runId);
    const [patch, statSummary] = await Promise.all([
        runShell("git", ["diff", "--binary"], { cwd: root, timeout: 30_000 }),
        runShell("git", ["diff", "--stat"], { cwd: root, timeout: 30_000 }),
    ]);
    if (!patch.failed) {
        await writeFile(paths.diffPatch, patch.stdout, "utf-8");
    }
    if (!statSummary.failed) {
        await writeFile(paths.diffStat, statSummary.stdout, "utf-8");
    }
    return paths;
}
export async function writeTestEvidenceLog(root, runId, lines) {
    const paths = await ensureCompletionArtifactContract(root, runId);
    await writeFile(paths.testLog, `${lines.join("\n")}\n`, "utf-8");
    return paths;
}
export async function getCompletionArtifactStatus(root, runId) {
    const paths = await ensureCompletionArtifactContract(root, runId);
    const missing = [];
    if (!(await hasNonEmptyFile(paths.diffPatch)))
        missing.push("diff artifact missing");
    if (!(await hasNonEmptyFile(paths.testLog)))
        missing.push("test artifact missing");
    if (!(await hasNonEmptyFile(paths.runLog)))
        missing.push("run log missing");
    if (!(await hasNonEmptyFile(paths.resultJson)))
        missing.push("result json missing");
    return { ok: missing.length === 0, missing, paths };
}
async function hasNonEmptyFile(path) {
    if (!(await pathExists(path)))
        return false;
    try {
        const info = await stat(path);
        return info.isFile() && info.size > 0;
    }
    catch {
        return false;
    }
}
