"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderMilestoneOverviewMarkdown = renderMilestoneOverviewMarkdown;
exports.assertExecutableScaffoldEvidence = assertExecutableScaffoldEvidence;
const errors_1 = require("../../errors");
const authoring_policy_1 = require("../authoring-policy");
const governed_hierarchy_context_1 = require("../governed-hierarchy-context");
const runtime_acceptance_1 = require("../runtime-acceptance");
const workflow_provenance_1 = require("../workflow-provenance");
const support_rendering_1 = require("./support-rendering");
function renderMilestoneOverviewMarkdown(options) {
    const workflowProvenance = (0, workflow_provenance_1.buildCliWorkflowProvenance)(options.workflowCliCommand
        ?? `day-shift milestone new --phase-overview ${options.phaseOverviewPath} --milestone-slug ${options.milestoneSlug}`);
    const upstreamSpecs = options.upstreamSpecs
        .map((entry) => {
        return [
            `    - id: ${entry.id}`,
            `      depth: ${entry.depth}`,
            `      ref: ${JSON.stringify(entry.ref)}`,
            `      source_type: ${JSON.stringify(entry.sourceType)}`
        ].join("\n");
    })
        .join("\n");
    const text = (0, runtime_acceptance_1.injectRuntimeAcceptanceFrontMatter)([
        "---",
        "artifact_schema_version: 1",
        "artifact_type: milestone_overview",
        `slice: ${options.slice}`,
        `phase: ${options.phase}`,
        `milestone: ${options.milestoneSlug}`,
        `planning_depth: ${options.planningDepth}`,
        ...(0, governed_hierarchy_context_1.renderGovernedHierarchyPolicyLines)(options.governedPolicy),
        `workflow_provenance: ${workflowProvenance.workflowProvenance}`,
        `workflow_cli_command: ${JSON.stringify(workflowProvenance.workflowCliCommand)}`,
        "status: draft",
        "upstream:",
        "  specs:",
        upstreamSpecs,
        `  slice_overview: ${options.sliceOverviewPath}`,
        `  phase_overview: ${options.phaseOverviewPath}`,
        "---",
        "",
        `# ${options.milestoneTitle}`,
        "",
        "## Purpose",
        "",
        (0, support_rendering_1.renderMilestonePurpose)(options),
        "",
        "## Scope",
        "",
        ...(0, support_rendering_1.renderMilestoneScope)(options),
        "",
        "## Navigation",
        "",
        `- ${options.phaseOverviewPath}`,
        `- ${options.sliceOverviewPath}`,
        "",
        "## Tasks",
        "",
        `Target task count: ${options.authoredMilestoneScaffold.taskCount}. Decomposition rationale: ${(0, support_rendering_1.verbPolicySafeProse)(options.authoredMilestoneScaffold.decompositionRationale)}`,
        "",
        ...(0, support_rendering_1.renderMilestoneTaskCandidates)(options.authoredMilestoneScaffold.tasks),
        "",
        "## Acceptance Criteria",
        "",
        ...(0, support_rendering_1.renderMilestoneAcceptanceCriteria)(options),
        "",
        "## Dependencies",
        "",
        ...(0, support_rendering_1.renderMilestoneDependencies)(options),
        "",
        "## Risks",
        "",
        ...(0, support_rendering_1.renderMilestoneRisks)(options),
        "",
        "## Validation Focus",
        "",
        ...(0, support_rendering_1.renderMilestoneValidationFocus)(options),
        "",
        "## Implementation Target Paths",
        "",
        ...options.scaffoldEvidence.targetPaths.map((entry) => `- ${entry}`),
        "",
        "## Validation Commands",
        "",
        "```sh",
        ...options.scaffoldEvidence.validationCommands,
        "```",
        "",
        "## Rollback / Recovery",
        "",
        ...options.scaffoldEvidence.rollback.map((entry) => `- ${entry}`),
        "",
        "## Scaffold Evidence",
        "",
        ...(0, support_rendering_1.renderScaffoldEvidenceLines)(options.scaffoldEvidence, options.authoredMilestoneScaffold),
        "",
        "## Open Questions",
        "",
        "- None currently.",
        ""
    ].join("\n"), options.runtimeAcceptance, { includeBreakoutPolicy: true });
    (0, support_rendering_1.assertPlanningAuthoringCompliance)("milestone_overview", text);
    return text;
}
function assertExecutableScaffoldEvidence(parentPath, evidence) {
    if (evidence.complete)
        return;
    const findings = (0, authoring_policy_1.projectPlanningScaffoldRefinementFindings)(parentPath, evidence);
    throw (0, errors_1.createInvalidConfigError)(`Planning scaffold refinement is required before milestone '${evidence.childSlug}' can be created.`, [
        ...findings.map((finding) => `${finding.path}: ${finding.message}`),
        "No milestone artifact write was attempted."
    ], [...findings]);
}
