import crypto from "node:crypto";
import { TASK_STATE_EVENT_ENVELOPE_SCHEMA_VERSION } from "../task-state-events/types.js";
function baseActor(ctx) {
    return { id: ctx.actorId?.trim() || "workspace-kit", source: "system" };
}
function draftPlanningEnvelope(kind, payload, ctx, options) {
    const event = {
        schemaVersion: TASK_STATE_EVENT_ENVELOPE_SCHEMA_VERSION,
        eventId: `evt-${crypto.randomUUID()}`,
        sequence: 0,
        parentEventId: null,
        recordedAt: new Date().toISOString(),
        actor: baseActor(ctx),
        command: {
            name: ctx.commandName,
            moduleId: ctx.moduleId ?? "task-engine",
            invocationId: ctx.invocationId
        },
        kind,
        payload
    };
    if (ctx.clientMutationId?.trim()) {
        event.clientMutationId = ctx.clientMutationId.trim();
    }
    if (ctx.phaseKey?.trim() || ctx.gitHeadSha?.trim()) {
        event.workspace = {
            ...(ctx.gitHeadSha ? { gitHeadSha: ctx.gitHeadSha } : {}),
            ...(ctx.phaseKey ? { phaseKey: ctx.phaseKey } : {})
        };
    }
    if (options?.expectedWorkspaceRevision !== undefined) {
        event.expectedWorkspaceRevision = options.expectedWorkspaceRevision;
    }
    return event;
}
export function draftPlanningPhaseCatalogUpsertedEvent(args) {
    return draftPlanningEnvelope("planning.phase_catalog.upserted", {
        phaseKey: args.phaseKey,
        shortDescription: args.shortDescription,
        updatedAt: args.updatedAt
    }, args.ctx);
}
export function draftPlanningPhaseCatalogRemovedEvent(args) {
    return draftPlanningEnvelope("planning.phase_catalog.removed", { phaseKey: args.phaseKey }, args.ctx);
}
export function draftPlanningWorkspaceStatusUpdatedEvent(args) {
    return draftPlanningEnvelope("planning.workspace_status.updated", {
        patch: args.patch,
        after: {
            workspaceRevision: args.after.workspaceRevision,
            currentKitPhase: args.after.currentKitPhase,
            nextKitPhase: args.after.nextKitPhase,
            activeFocus: args.after.activeFocus,
            lastUpdated: args.after.lastUpdated,
            blockers: [...args.after.blockers],
            pendingDecisions: [...args.after.pendingDecisions],
            nextAgentActions: [...args.after.nextAgentActions],
            updatedAt: args.after.updatedAt
        },
        payloadDigest: args.payloadDigest,
        ...(Object.hasOwn(args, "previousCurrentKitPhase")
            ? { previousCurrentKitPhase: args.previousCurrentKitPhase ?? null }
            : {})
    }, args.ctx, { expectedWorkspaceRevision: args.before.workspaceRevision });
}
export function draftPlanningPhaseNoteCreatedEvent(args) {
    return draftPlanningEnvelope("planning.phase_note.created", { note: args.note }, args.ctx);
}
export function draftPlanningPhaseNoteUpdatedEvent(args) {
    return draftPlanningEnvelope("planning.phase_note.updated", { note: args.note }, args.ctx);
}
export function draftPlanningPhaseNoteArchivedEvent(args) {
    return draftPlanningEnvelope("planning.phase_note.archived", { note: args.note }, args.ctx);
}
export function draftPlanningPhaseNoteSuggestionCreatedEvent(args) {
    return draftPlanningEnvelope("planning.phase_note_suggestion.created", { suggestion: args.suggestion }, args.ctx);
}
export function draftPlanningPhaseNoteSuggestionUpdatedEvent(args) {
    return draftPlanningEnvelope("planning.phase_note_suggestion.updated", { suggestion: args.suggestion }, args.ctx);
}
export function draftPlanningPhaseNoteSuggestionRemovedEvent(args) {
    return draftPlanningEnvelope("planning.phase_note_suggestion.removed", { suggestionId: args.suggestionId, noteId: args.noteId }, args.ctx);
}
export function draftPlanningIdeaCreatedEvent(args) {
    return draftPlanningEnvelope("planning.idea.created", { idea: args.idea }, args.ctx);
}
export function draftPlanningIdeaUpdatedEvent(args) {
    return draftPlanningEnvelope("planning.idea.updated", { idea: args.idea, ...(args.removed ? { removed: true } : {}) }, args.ctx);
}
export function draftPlanningModuleStateUpdatedEvent(args) {
    return draftPlanningEnvelope("planning.module_state.updated", {
        moduleId: args.moduleId,
        stateSchemaVersion: args.stateSchemaVersion,
        state: args.state,
        updatedAt: args.updatedAt,
        ...(args.expectedStateSchemaVersion !== undefined
            ? { expectedStateSchemaVersion: args.expectedStateSchemaVersion }
            : {}),
        ...(args.removed ? { removed: true } : {})
    }, args.ctx);
}
