import GenesisEquivalenceProofFailure from "../../../../src/domain/migrations/GenesisEquivalenceProofFailure.js";
import { GRAPH_MODEL_MIGRATION_RUNTIME_REPLAY_FAILED, } from "../../../../src/domain/migrations/GraphModelMigrationRuntimeReplayResult.js";
import { formatGraphModelMigrationCommandReport } from "./GraphModelMigrationCommandReport.js";
import { V17GoldenGraphFixtureWetRunHarnessResult } from "./V17GoldenGraphFixtureWetRunHarness.js";
/** Formats deterministic operator evidence for a v17 fixture wet run. */
export function formatV17GoldenGraphFixtureWetRunReport(result) {
    const checkedResult = requireHarnessResult(result);
    return [
        'git-warp v18 v17 fixture wet-run report',
        `fixtureId: ${checkedResult.restoreResult.manifest.fixtureId}`,
        `graphId: ${checkedResult.restoreResult.manifest.graphId}`,
        `sourceVersion: ${checkedResult.restoreResult.manifest.sourceVersion}`,
        `restoredRefs: ${checkedResult.restoreResult.restoredRefs.length}`,
        ...restoredRefLines(checkedResult),
        ...commandLines(checkedResult),
        ...mismatchLines(checkedResult),
        ...runtimeReplayLines(checkedResult),
        ...driftCheckLines(checkedResult),
    ].join('\n');
}
function restoredRefLines(result) {
    return Object.freeze(result.restoreResult.restoredRefs.map((ref) => `restoredRef: ${ref.refName} ${ref.head} patches=${ref.patchCount}`));
}
function commandLines(result) {
    return Object.freeze(formatGraphModelMigrationCommandReport(result.commandResult)
        .split('\n')
        .map((line) => `command.${line}`));
}
function mismatchLines(result) {
    const gateResult = result.commandResult.gateResult;
    if (gateResult === null || !(gateResult.proofResult instanceof GenesisEquivalenceProofFailure)) {
        return Object.freeze([]);
    }
    return Object.freeze([
        'mismatches:',
        ...gateResult.proofResult.mismatches.map(formatMismatchLine),
    ]);
}
function formatMismatchLine(mismatch) {
    return [
        '-',
        mismatch.kind,
        mismatch.factKind,
        displayValue(mismatch.factKey),
        displayValue(mismatch.fieldPath),
        `legacy=${displayNullable(mismatch.legacyValue)}`,
        `migrated=${displayNullable(mismatch.migratedValue)}`,
    ].join(' ');
}
function runtimeReplayLines(result) {
    const runtimeReplay = result.runtimeReplayResult;
    if (runtimeReplay === null) {
        return Object.freeze(['runtimeReplay: skipped']);
    }
    const lines = [
        `runtimeReplay: ${runtimeReplay.status}`,
        `runtimeReplayOperations: ${runtimeReplay.replayedOperationCount}`,
        `runtimeReplayWitness: ${runtimeReplay.witness}`,
    ];
    if (runtimeReplay.status === GRAPH_MODEL_MIGRATION_RUNTIME_REPLAY_FAILED) {
        lines.push('runtimeReplayFatalErrors:');
        lines.push(...fatalNoticeLines(runtimeReplay.fatalErrors));
    }
    return Object.freeze(lines);
}
function driftCheckLines(result) {
    const lines = [
        `driftCheck: ${result.driftCheckResult.status}`,
        `driftCheckedRefs: ${result.driftCheckResult.checkedRefCount}`,
    ];
    if (result.driftCheckResult.fatalErrors.length > 0) {
        lines.push('driftCheckFatalErrors:');
        lines.push(...fatalNoticeLines(result.driftCheckResult.fatalErrors));
    }
    return Object.freeze(lines);
}
function fatalNoticeLines(fatalErrors) {
    return Object.freeze(fatalErrors.map((notice) => `- ${notice.code}: ${notice.message}`));
}
function displayNullable(value) {
    if (value === null) {
        return '(none)';
    }
    return displayValue(value);
}
function displayValue(value) {
    return value
        .replaceAll('\0', '\\0')
        .replaceAll('\n', '\\n');
}
function requireHarnessResult(result) {
    if (!(result instanceof V17GoldenGraphFixtureWetRunHarnessResult)) {
        throw new V17GoldenGraphFixtureWetRunReportError('result must be a V17GoldenGraphFixtureWetRunHarnessResult');
    }
    return result;
}
export class V17GoldenGraphFixtureWetRunReportError extends Error {
    constructor(message) {
        super(message);
        this.name = 'V17GoldenGraphFixtureWetRunReportError';
    }
}
