"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLightweightTaskWorkspace = createLightweightTaskWorkspace;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const errors_1 = require("../../errors");
const command_write_summary_1 = require("../../license/command-write-summary");
const front_matter_utils_1 = require("../front-matter-utils");
const scaffold_application_1 = require("../scaffold-application");
const upstream_specs_1 = require("../upstream-specs");
const artifact_parsing_1 = require("./artifact-parsing");
const artifact_rendering_1 = require("./artifact-rendering");
function createLightweightTaskWorkspace(repositoryRoot, options) {
    const sliceOverviewPath = (0, artifact_parsing_1.resolveOverviewPath)(repositoryRoot, options.sliceOverviewPath, "--slice-overview");
    if (!node_fs_1.default.existsSync(sliceOverviewPath.absolute)) {
        throw (0, errors_1.createUsageError)(`Parent slice overview not found at '${sliceOverviewPath.relative}'.`, ["Run day-shift slice new first or pass the correct --slice-overview path."]);
    }
    const parsedSliceOverview = (0, artifact_parsing_1.parseSliceOverview)(sliceOverviewPath.absolute);
    const upstreamSpecs = (0, upstream_specs_1.normalizeUpstreamSpecsToLocalSpecPaths)(repositoryRoot, parsedSliceOverview.upstreamSpecs);
    if (parsedSliceOverview.planningDepth !== "lightweight") {
        throw (0, errors_1.createUsageError)(`Parent slice overview '${sliceOverviewPath.relative}' must use planning_depth: lightweight for --lightweight task creation.`);
    }
    const taskSlug = (0, artifact_parsing_1.resolveTaskSlug)(options.taskSlug);
    const taskTitle = (0, artifact_parsing_1.resolveTaskTitle)(taskSlug, options.taskTitle);
    const sliceDirectoryPath = node_path_1.default.dirname(sliceOverviewPath.absolute);
    const tasksDirectoryPath = node_path_1.default.join(sliceDirectoryPath, "tasks");
    const taskDirectoryPath = node_path_1.default.join(tasksDirectoryPath, taskSlug);
    const taskDefinitionPath = node_path_1.default.join(taskDirectoryPath, "task-definition.md");
    const implementationSummaryPath = node_path_1.default.join(taskDirectoryPath, "implementation-summary.md");
    const generatedAt = new Date().toISOString();
    const tasksDirectoryAction = node_fs_1.default.existsSync(tasksDirectoryPath) ? "kept" : "created";
    const taskDefinitionExists = node_fs_1.default.existsSync(taskDefinitionPath);
    const renderedTaskDefinition = taskDefinitionExists
        ? null
        : (0, artifact_rendering_1.renderLightweightTaskDefinitionMarkdown)({
            repositoryRoot,
            taskTitle,
            taskSlug,
            slice: parsedSliceOverview.slice,
            planningDepth: parsedSliceOverview.planningDepth,
            sliceOverviewPath: sliceOverviewPath.relative,
            upstreamSpecs,
            governedPolicy: parsedSliceOverview.governedPolicy,
            generatedAt
        });
    const implementationSummaryExists = node_fs_1.default.existsSync(implementationSummaryPath);
    const renderedImplementationSummary = implementationSummaryExists
        ? null
        : (0, artifact_rendering_1.renderLightweightImplementationSummaryMarkdown)({
            repositoryRoot,
            taskTitle,
            taskSlug,
            slice: parsedSliceOverview.slice,
            planningDepth: parsedSliceOverview.planningDepth,
            sliceOverviewPath: sliceOverviewPath.relative,
            taskDefinitionPath: (0, front_matter_utils_1.toRepositoryLikePath)(node_path_1.default.relative(repositoryRoot, taskDefinitionPath)),
            upstreamSpecs,
            governedPolicy: parsedSliceOverview.governedPolicy,
            generatedAt
        });
    const taskDefinitionRelativePath = (0, front_matter_utils_1.toRepositoryLikePath)(node_path_1.default.relative(repositoryRoot, taskDefinitionPath));
    const implementationSummaryRelativePath = (0, front_matter_utils_1.toRepositoryLikePath)(node_path_1.default.relative(repositoryRoot, implementationSummaryPath));
    const application = (0, scaffold_application_1.applyPlanningScaffoldSync)(repositoryRoot, [
        ...(!taskDefinitionExists && renderedTaskDefinition !== null
            ? [{ path: taskDefinitionRelativePath, contents: renderedTaskDefinition }]
            : []),
        ...(!implementationSummaryExists && renderedImplementationSummary !== null
            ? [{ path: implementationSummaryRelativePath, contents: renderedImplementationSummary }]
            : [])
    ], { parentPaths: [sliceOverviewPath.relative] });
    (0, scaffold_application_1.assertPlanningScaffoldApplied)(application);
    return {
        repositoryRoot,
        slice: parsedSliceOverview.slice,
        planningDepth: parsedSliceOverview.planningDepth,
        sliceOverviewPath: sliceOverviewPath.relative,
        tasksDirectoryPath,
        taskDirectoryPath,
        taskDefinitionPath,
        implementationSummaryPath,
        taskSlug,
        taskTitle,
        tasksDirectoryAction,
        taskDefinitionAction: taskDefinitionExists ? "kept" : "created",
        implementationSummaryAction: implementationSummaryExists ? "kept" : "created",
        contributingSpecIds: upstreamSpecs.map((entry) => entry.id),
        requiresRefinement: true,
        application,
        commandWriteSummary: (0, command_write_summary_1.normalizeCommandWriteSummary)({
            commandFamily: "task_generation",
            primaryOutputArtifacts: [taskDefinitionRelativePath, implementationSummaryRelativePath],
            updatedArtifacts: [
                {
                    path: taskDefinitionRelativePath,
                    action: taskDefinitionExists ? "kept" : "created",
                    reason: taskDefinitionExists ? "no_write" : "accepted_create"
                },
                {
                    path: implementationSummaryRelativePath,
                    action: implementationSummaryExists ? "kept" : "created",
                    reason: implementationSummaryExists ? "no_write" : "accepted_create"
                }
            ]
        })
    };
}
