// engine/dashboard/spec-updater.ts — PATCH handler logic for spec status updates (SPEC-122)
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { isValidStatus, isValidTransition, buildUpdateResult } from './kanban-logic.js';
/** Root data directory for all project data */
const DATA_DIR = join(process.cwd(), 'data');
/** Update a spec's status after validating the transition */
export async function updateSpecStatus(projectId, specId, newStatus) {
    if (!isValidStatus(newStatus)) {
        return buildUpdateResult(false, specId, newStatus, `Invalid status: ${newStatus}`);
    }
    const specFile = join(DATA_DIR, 'projects', projectId, 'specs', specId, 'spec.json');
    let raw;
    try {
        const content = await readFile(specFile, 'utf-8');
        raw = JSON.parse(content);
    }
    catch {
        return buildUpdateResult(false, specId, newStatus, `Spec not found: ${specId}`);
    }
    const currentStatus = raw.status ?? 'draft';
    if (!isValidStatus(currentStatus)) {
        return buildUpdateResult(false, specId, newStatus, `Current status is invalid: ${currentStatus}`);
    }
    if (!isValidTransition(currentStatus, newStatus)) {
        return buildUpdateResult(false, specId, newStatus, `Invalid transition: ${currentStatus} → ${newStatus}`);
    }
    raw.status = newStatus;
    raw.updatedAt = new Date().toISOString();
    await writeFile(specFile, JSON.stringify(raw, null, 2), 'utf-8');
    return buildUpdateResult(true, specId, newStatus);
}
/** Type guard to check if an update result indicates success */
export function isUpdateSuccess(result) {
    return result.success;
}
//# sourceMappingURL=spec-updater.js.map