// engine/dashboard/spec-updater.ts — PATCH handler logic for spec status updates (SPEC-122)
// SPEC-720: All status writes now route through handleUpdateStatus (full gates).
import { isValidStatus, buildUpdateResult } from './kanban-logic.js';
import { handleUpdateStatus } from '../../tools/update-status/index.js';
/** Update a spec's status after validating the transition.
 *
 * SPEC-720: Routes through handleUpdateStatus so all gates (DoD/validate/QA) run.
 * Clients that previously relied on the old direct JSON write may now receive
 * gate-blocked responses (success: false with a gate reason).
 */
export async function updateSpecStatus(projectId, specId, newStatus, projectPath) {
    if (!isValidStatus(newStatus)) {
        return buildUpdateResult(false, specId, newStatus, `Invalid status: ${newStatus}`);
    }
    // Resolve projectPath from projectId if not supplied
    const resolvedPath = projectPath ??
        (() => {
            // Best-effort: hashProjectPath is not reversible; callers should pass projectPath.
            // Fall back to cwd which is where the server runs.
            return process.cwd();
        })();
    try {
        const result = await handleUpdateStatus({
            specId,
            status: newStatus,
            projectId,
            projectPath: resolvedPath,
            trigger: 'dashboard',
            actor: 'dashboard',
        });
        if (result.isError) {
            const reason = result.content[0]?.type === 'text' ? result.content[0].text : 'gate blocked';
            return buildUpdateResult(false, specId, newStatus, reason);
        }
        return buildUpdateResult(true, specId, newStatus);
    }
    catch (err) {
        const reason = err instanceof Error ? err.message : String(err);
        return buildUpdateResult(false, specId, newStatus, `Unexpected error: ${reason}`);
    }
}
/** Type guard to check if an update result indicates success */
export function isUpdateSuccess(result) {
    return result.success;
}
//# sourceMappingURL=spec-updater.js.map