import { emitAutomationJobAutoDisabled, emitAutomationJobCreated, emitAutomationJobDisabled, emitAutomationJobEnabled, emitAutomationJobUpdated, emitAutomationRunCompleted, emitAutomationRunFailed, emitAutomationRunQueued, emitAutomationRunStarted, } from '../runtime/emitters/index.js';
function automationEmitterContext(traceId, sessionId) {
    return {
        sessionId: sessionId ?? 'automation',
        source: 'automation-manager',
        traceId,
    };
}
export function emitAutomationManagerJobCreated(runtimeBus, job) {
    if (!runtimeBus)
        return;
    emitAutomationJobCreated(runtimeBus, automationEmitterContext(job.id, job.execution.target.sessionId), {
        jobId: job.id,
        name: job.name,
        scheduleKind: job.schedule.kind,
        enabled: job.enabled,
    });
}
export function emitAutomationManagerJobUpdated(runtimeBus, job, changedFields) {
    if (!runtimeBus)
        return;
    emitAutomationJobUpdated(runtimeBus, automationEmitterContext(job.id, job.execution.target.sessionId), {
        jobId: job.id,
        changedFields,
    });
    if (job.enabled) {
        emitAutomationJobEnabled(runtimeBus, automationEmitterContext(job.id, job.execution.target.sessionId), {
            jobId: job.id,
        });
    }
    else {
        emitAutomationJobDisabled(runtimeBus, automationEmitterContext(job.id, job.execution.target.sessionId), {
            jobId: job.id,
            reason: job.pausedReason ?? 'disabled',
        });
    }
}
export function emitAutomationManagerJobAutoDisabled(runtimeBus, job, reason) {
    if (!runtimeBus)
        return;
    emitAutomationJobAutoDisabled(runtimeBus, automationEmitterContext(job.id, job.execution.target.sessionId), {
        jobId: job.id,
        reason,
        consecutiveFailures: job.failureCount,
    });
}
export function emitAutomationManagerRunQueued(runtimeBus, job, run) {
    if (!runtimeBus)
        return;
    emitAutomationRunQueued(runtimeBus, automationEmitterContext(run.id, job.execution.target.sessionId), {
        jobId: job.id,
        runId: run.id,
        scheduledAt: run.queuedAt,
        forced: run.forceRun,
    });
}
export function emitAutomationManagerRunStarted(runtimeBus, job, run) {
    if (!runtimeBus || run.startedAt === undefined)
        return;
    emitAutomationRunStarted(runtimeBus, automationEmitterContext(run.id, job.execution.target.sessionId), {
        jobId: job.id,
        runId: run.id,
        startedAt: run.startedAt,
        attempt: run.attempt,
    });
}
export function emitAutomationManagerRunCompleted(runtimeBus, job, run, outcome) {
    if (!runtimeBus || run.startedAt === undefined || run.endedAt === undefined)
        return;
    emitAutomationRunCompleted(runtimeBus, automationEmitterContext(run.id, job.execution.target.sessionId), {
        jobId: job.id,
        runId: run.id,
        startedAt: run.startedAt,
        completedAt: run.endedAt,
        durationMs: run.durationMs ?? Math.max(0, run.endedAt - run.startedAt),
        outcome,
    });
}
export function emitAutomationManagerRunFailed(runtimeBus, job, run, error, retryable) {
    if (!runtimeBus || run.startedAt === undefined || run.endedAt === undefined)
        return;
    emitAutomationRunFailed(runtimeBus, automationEmitterContext(run.id, job.execution.target.sessionId), {
        jobId: job.id,
        runId: run.id,
        startedAt: run.startedAt,
        failedAt: run.endedAt,
        error,
        retryable,
    });
}
