import { attachPolicyMeta } from "../attach-planning-response-meta.js";
import { readWorkspaceStatusSnapshotFromDual } from "../persistence/workspace-status-store.js";
import { resolveCanonicalPhase } from "../phase-resolution.js";
import { strictValidationError } from "./strict-store-validation.js";
import { runAssignTaskPhase, runClearTaskPhase } from "../task-engine-phase-mutations.js";
/**
 * Phase field mutations on task rows.
 * Returns **`null`** when the command name is not handled here.
 */
export async function resolveTaskPhaseCommands(command, ctx, planning, store) {
    const args = command.args ?? {};
    if (command.name === "assign-task-phase") {
        const actor = typeof args.actor === "string"
            ? args.actor
            : ctx.resolvedActor !== undefined
                ? ctx.resolvedActor
                : undefined;
        const workspaceStatus = readWorkspaceStatusSnapshotFromDual(planning.sqliteDual);
        const phaseRes = resolveCanonicalPhase({
            effectiveConfig: ctx.effectiveConfig,
            workspaceStatus
        });
        const r = await runAssignTaskPhase({
            store,
            ctx,
            planning,
            strictValidationError,
            actor,
            rawArgs: args,
            canonicalWorkspacePhaseKey: phaseRes.canonicalPhaseKey
        });
        if (r.ok && r.data && typeof r.data === "object") {
            attachPolicyMeta(r.data, ctx, planning.sqliteDual.getPlanningGeneration());
        }
        return r;
    }
    if (command.name === "clear-task-phase") {
        const actor = typeof args.actor === "string"
            ? args.actor
            : ctx.resolvedActor !== undefined
                ? ctx.resolvedActor
                : undefined;
        const r = await runClearTaskPhase({
            store,
            ctx,
            planning,
            strictValidationError,
            actor,
            rawArgs: args
        });
        if (r.ok && r.data && typeof r.data === "object") {
            attachPolicyMeta(r.data, ctx, planning.sqliteDual.getPlanningGeneration());
        }
        return r;
    }
    return null;
}
