"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.explainWorkflowForArtifact = explainWorkflowForArtifact;
const node_path_1 = __importDefault(require("node:path"));
const cli_constants_1 = require("../../config/cli-constants");
const config_contract_1 = require("../../config/config-contract");
const work_discovery_1 = require("../work-discovery");
const artifact_discovery_1 = require("./artifact-discovery");
const document_support_1 = require("./document-support");
const gap_analysis_1 = require("./gap-analysis");
function explainWorkflowForArtifact(options) {
    const scope = (0, artifact_discovery_1.resolveScope)(options.repositoryRoot, options.artifactPath, false);
    const record = (0, artifact_discovery_1.readArtifactRecord)(options.repositoryRoot, scope.path);
    const resolvedConfig = (0, config_contract_1.resolveDayShiftConfig)({
        repositoryContext: { root: options.repositoryRoot, detectionSource: "explicit", warnedAboutFallback: false }
    });
    switch (record.artifactType) {
        case "work_overview":
            return {
                artifactPath: record.path,
                artifactType: record.artifactType,
                workflowPath: cli_constants_1.CLI_SOURCE_SPEC_PLANNING_WORKFLOW,
                promptPath: null,
                promptResolution: null,
                explanation: "Canonical work overviews are non-actionable parent records. Select a declared slice or task beneath this work root for lifecycle activity.",
                recommendation: "Inspect declared canonical children without selecting the work overview itself.",
                recommendedCommand: `node dist/cli/index.js planning inventory --root ${node_path_1.default.posix.dirname(record.path)}`,
                nextBoundary: "Choose a declared child planning artifact; the work overview is context only."
            };
        case "task_definition": {
            const workEvidence = (0, work_discovery_1.projectCanonicalWorkCandidate)({
                repositoryRoot: options.repositoryRoot,
                artifactPath: record.path
            });
            const summaryRecord = (0, artifact_discovery_1.readArtifactIfExists)(options.repositoryRoot, (0, document_support_1.pairedImplementationSummaryPath)(record.path));
            const topologyEvidence = (0, artifact_discovery_1.projectPlanningTopologyEvidence)(record);
            if (summaryRecord !== null && (0, document_support_1.isCompletedImplementationSummary)(summaryRecord)) {
                const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, ".day-shift/prompts/user/implementation.next.md");
                if (topologyEvidence.workflowTopology === null) {
                    return {
                        artifactPath: record.path,
                        artifactType: record.artifactType,
                        workflowPath: ".day-shift/workflows/04-summarize-and-reconcile.md",
                        promptPath: promptResolution.resolvedPromptPath,
                        promptResolution,
                        explanation: "Completed task evidence has invalid or contradictory workflow topology, so no downstream review or reconciliation boundary can be inferred.",
                        recommendation: "Repair the typed topology findings before selecting a closeout route.",
                        recommendedCommand: `node dist/cli/index.js task review --task-definition ${record.path}`,
                        nextBoundary: "Use read-only task review evidence to repair topology; no reconciliation command is available from this state.",
                        workEvidence,
                        nextAction: workEvidence?.nextAction ?? null,
                        topologyEvidence
                    };
                }
                const requiresReconciliation = topologyEvidence.reconciliationRequired === true;
                return {
                    artifactPath: record.path,
                    artifactType: record.artifactType,
                    workflowPath: ".day-shift/workflows/04-summarize-and-reconcile.md",
                    promptPath: promptResolution.resolvedPromptPath,
                    promptResolution,
                    explanation: requiresReconciliation
                        ? "Completed task execution evidence is present at the topology-defined milestone-reconciliation boundary."
                        : `Completed task execution evidence is present at the topology-defined direct ${topologyEvidence.taskSummaryReviewBoundary} boundary; no milestone reconciliation applies.`,
                    recommendation: "Review the paired implementation summary as read-only closeout evidence.",
                    recommendedCommand: `node dist/cli/index.js implementation-summary review --task-definition ${record.path}`,
                    nextBoundary: requiresReconciliation
                        ? "Review the paired implementation summary, then continue into milestone reconciliation without auto-closing metadata."
                        : "Review the paired implementation summary directly; no reconciliation artifact or hierarchy write is required.",
                    workEvidence,
                    nextAction: workEvidence?.nextAction ?? null,
                    topologyEvidence
                };
            }
            const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, ".day-shift/prompts/user/task.next.md");
            return {
                artifactPath: record.path,
                artifactType: record.artifactType,
                workflowPath: ".day-shift/workflows/03-implement-from-task.md",
                promptPath: promptResolution.resolvedPromptPath,
                promptResolution,
                explanation: workEvidence?.nextAction.nextAction === "implement"
                    ? "This task definition is implementation-ready, so the task implementation workflow applies."
                    : "Task definitions route through the implement-from-task workflow, which begins with a readiness gate before code changes.",
                recommendation: workEvidence?.nextAction.nextAction === "implement"
                    ? "Implement the task using the task handoff workflow."
                    : "Re-run the readiness gate before implementation.",
                recommendedCommand: workEvidence?.nextAction.nextAction === "implement"
                    ? null
                    : `node dist/cli/index.js task readiness-review --task-definition ${record.path}`,
                nextBoundary: "Run the readiness check before code changes, then implement within scope and update the paired implementation summary plus task-definition closeout metadata.",
                workEvidence,
                nextAction: workEvidence?.nextAction ?? null
            };
        }
        case "implementation_summary": {
            const topologyEvidence = (0, artifact_discovery_1.projectPlanningTopologyEvidence)(record);
            const taskTarget = (0, artifact_discovery_1.inspectTaskCommandArtifactTarget)(options.repositoryRoot, record.upstreamTaskDefinitionPath);
            const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, (0, document_support_1.isCompletedImplementationSummary)(record)
                ? ".day-shift/prompts/user/implementation.review.md"
                : ".day-shift/prompts/user/implementation.build.md");
            if (topologyEvidence.workflowTopology === null) {
                return {
                    artifactPath: record.path,
                    artifactType: record.artifactType,
                    workflowPath: ".day-shift/workflows/04-summarize-and-reconcile.md",
                    promptPath: promptResolution.resolvedPromptPath,
                    promptResolution,
                    explanation: "The implementation summary has invalid or contradictory workflow topology, so no downstream review or reconciliation boundary can be inferred.",
                    recommendation: "Repair the typed topology findings before selecting a closeout route.",
                    recommendedCommand: taskTarget.callable
                        ? `node dist/cli/index.js implementation-summary review --task-definition ${taskTarget.normalizedPath}`
                        : null,
                    nextBoundary: "Use read-only implementation-summary review evidence to repair topology; no reconciliation command is available from this state.",
                    topologyEvidence
                };
            }
            const requiresReconciliation = topologyEvidence.reconciliationRequired === true;
            return {
                artifactPath: record.path,
                artifactType: record.artifactType,
                workflowPath: ".day-shift/workflows/04-summarize-and-reconcile.md",
                promptPath: promptResolution.resolvedPromptPath,
                promptResolution,
                explanation: requiresReconciliation
                    ? "This implementation summary uses the topology-defined milestone-reconciliation review boundary."
                    : `This implementation summary uses the topology-defined direct ${topologyEvidence.taskSummaryReviewBoundary} boundary with no reconciliation artifact.`,
                recommendation: (0, document_support_1.isCompletedImplementationSummary)(record)
                    ? requiresReconciliation
                        ? "Review implementation evidence before milestone rollup."
                        : "Review implementation evidence at the direct summary boundary."
                    : "Complete the task execution summary.",
                recommendedCommand: taskTarget.callable
                    ? `node dist/cli/index.js implementation-summary ${(0, document_support_1.isCompletedImplementationSummary)(record) ? "review" : "build"} --task-definition ${taskTarget.normalizedPath}`
                    : null,
                nextBoundary: (0, document_support_1.isCompletedImplementationSummary)(record)
                    ? requiresReconciliation
                        ? "Use implementation-summary review for read-only evaluation, then continue into milestone reconciliation when the milestone is ready."
                        : "Use implementation-summary review as the direct read-only closeout boundary; no milestone reconciliation applies."
                    : "Refresh the paired implementation summary evidence, then use implementation-summary review as the read-only validation boundary.",
                topologyEvidence
            };
        }
        case "milestone_overview": {
            const reconciliationRecord = (0, artifact_discovery_1.readArtifactIfExists)(options.repositoryRoot, (0, document_support_1.pairedReconciliationPath)(record.path));
            const allTaskSummariesComplete = (0, gap_analysis_1.areAllTaskImplementationSummariesComplete)(options.repositoryRoot, record.path);
            if (allTaskSummariesComplete) {
                const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, reconciliationRecord !== null && (0, document_support_1.isCompletedReconciliation)(reconciliationRecord)
                    ? ".day-shift/prompts/user/reconciliation.review.md"
                    : ".day-shift/prompts/user/reconciliation.build.md");
                return {
                    artifactPath: record.path,
                    artifactType: record.artifactType,
                    workflowPath: ".day-shift/workflows/04-summarize-and-reconcile.md",
                    promptPath: promptResolution.resolvedPromptPath,
                    promptResolution,
                    explanation: "All known task implementation summaries are complete, so milestone-level summarize-and-reconcile guidance applies.",
                    recommendation: reconciliationRecord !== null && (0, document_support_1.isCompletedReconciliation)(reconciliationRecord)
                        ? "Review milestone closure evidence."
                        : "Build or refresh the paired milestone reconciliation.",
                    recommendedCommand: `node dist/cli/index.js reconciliation ${reconciliationRecord !== null && (0, document_support_1.isCompletedReconciliation)(reconciliationRecord) ? "review" : "build"} --milestone-overview ${record.path}`,
                    nextBoundary: reconciliationRecord !== null && (0, document_support_1.isCompletedReconciliation)(reconciliationRecord)
                        ? "Use reconciliation review as read-only milestone closure evidence before any explicit metadata update."
                        : "Refresh or review the paired milestone reconciliation without treating that step as automatic closure authority."
                };
            }
            const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, ".day-shift/prompts/user/task.next.md");
            return {
                artifactPath: record.path,
                artifactType: record.artifactType,
                workflowPath: ".day-shift/workflows/03-implement-from-task.md",
                promptPath: promptResolution.resolvedPromptPath,
                promptResolution,
                explanation: "Milestone reconciliation is premature until all task implementation summaries are complete.",
                recommendation: "Advance the next task beneath this milestone.",
                recommendedCommand: `node dist/cli/index.js planning gap-report --scope ${record.path}`,
                nextBoundary: "Select or create the next task beneath this milestone before moving into implementation or reconciliation."
            };
        }
        case "reconciliation": {
            const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, (0, document_support_1.isCompletedReconciliation)(record)
                ? ".day-shift/prompts/user/reconciliation.review.md"
                : ".day-shift/prompts/user/reconciliation.build.md");
            return {
                artifactPath: record.path,
                artifactType: record.artifactType,
                workflowPath: ".day-shift/workflows/04-summarize-and-reconcile.md",
                promptPath: promptResolution.resolvedPromptPath,
                promptResolution,
                explanation: "Reconciliation artifacts are milestone review artifacts governed by the summarize-and-reconcile workflow.",
                recommendation: (0, document_support_1.isCompletedReconciliation)(record)
                    ? "Review reconciliation closure evidence."
                    : "Complete the milestone reconciliation.",
                recommendedCommand: record.upstreamMilestoneOverviewPath === null
                    ? null
                    : `node dist/cli/index.js reconciliation ${(0, document_support_1.isCompletedReconciliation)(record) ? "review" : "build"} --milestone-overview ${record.upstreamMilestoneOverviewPath}`,
                nextBoundary: (0, document_support_1.isCompletedReconciliation)(record)
                    ? "Keep further steps read-only unless a separate metadata update or follow-up task is explicitly chosen."
                    : "Complete or refresh the reconciliation contents, then use reconciliation review as the read-only closure check."
            };
        }
        case "phase_overview": {
            const phaseReadyForReview = (0, gap_analysis_1.areAllDeclaredMilestonesReconciled)(options.repositoryRoot, record);
            const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, phaseReadyForReview
                ? ".day-shift/prompts/user/phase.review.md"
                : ".day-shift/prompts/user/phase.next.md");
            return {
                artifactPath: record.path,
                artifactType: record.artifactType,
                workflowPath: phaseReadyForReview
                    ? ".day-shift/workflows/04-summarize-and-reconcile.md"
                    : cli_constants_1.CLI_SOURCE_SPEC_PLANNING_WORKFLOW,
                promptPath: promptResolution.resolvedPromptPath,
                promptResolution,
                explanation: phaseReadyForReview
                    ? "Every declared milestone has completed reconciliation evidence, so this phase is on the read-only phase review boundary even when overview status metadata is stale."
                    : "The phase still has declared milestone work without completed reconciliation evidence, so phase planning and milestone execution remain active.",
                recommendation: phaseReadyForReview
                    ? "Review phase closure evidence."
                    : "Inspect milestone coverage beneath this phase.",
                recommendedCommand: phaseReadyForReview
                    ? `node dist/cli/index.js phase review --phase-overview ${record.path}`
                    : `node dist/cli/index.js planning inventory --root ${node_path_1.default.posix.dirname(record.path)}`,
                nextBoundary: phaseReadyForReview
                    ? "Run read-only phase review before any separately authorized lifecycle metadata update."
                    : "Plan or inspect milestone structure here; downstream implementation happens only after a concrete task is selected."
            };
        }
        case "slice_overview": {
            const promptResolution = (0, artifact_discovery_1.resolveRequiredPromptReference)(options.repositoryRoot, resolvedConfig.values, ".day-shift/prompts/user/slice.next.md");
            return {
                artifactPath: record.path,
                artifactType: record.artifactType,
                workflowPath: cli_constants_1.CLI_SOURCE_SPEC_PLANNING_WORKFLOW,
                promptPath: promptResolution.resolvedPromptPath,
                promptResolution,
                explanation: "Slice overviews are planning-layer artifacts, so the planning workflow remains the relevant contract surface.",
                recommendation: "Inspect phase coverage beneath this slice.",
                recommendedCommand: `node dist/cli/index.js planning inventory --root ${node_path_1.default.posix.dirname(record.path)}`,
                nextBoundary: "Use this slice as planning context for phase selection, creation, or review rather than as an implementation handoff."
            };
        }
        default:
            return (0, document_support_1.assertNever)(record.artifactType);
    }
}
