import { logger } from '@pellux/goodvibes-sdk/platform/utils/logger';
import { summarizeError } from '@pellux/goodvibes-sdk/platform/utils/error-display';
export function scheduleAutomationFailureFollowUp(context, maybeDeliverFailureNotice, job, run) {
    maybeDeliverFailureNotice(job, run);
    if (!job.enabled)
        return;
    if (job.failure.action === 'cooldown') {
        const cooled = {
            ...job,
            nextRunAt: Date.now() + Math.max(1_000, job.failure.cooldownMs),
            updatedAt: Date.now(),
        };
        context.jobs.set(job.id, cooled);
        context.scheduleJob(cooled);
        void context.saveJobs();
        return;
    }
    const maxAttempts = Math.max(1, job.execution.maxAttempts ?? 1);
    if (job.failure.action !== 'retry' || run.attempt >= maxAttempts) {
        return;
    }
    const retryKey = `${job.id}:${run.id}`;
    const existing = context.retryTimers.get(retryKey);
    if (existing) {
        clearTimeout(existing);
    }
    const timer = setTimeout(() => {
        context.retryTimers.delete(retryKey);
        const latestJob = context.jobs.get(job.id);
        if (!latestJob?.enabled)
            return;
        if (context.activeRunCount() >= context.maxConcurrentRuns()) {
            scheduleAutomationFailureFollowUp(context, maybeDeliverFailureNotice, latestJob, run);
            return;
        }
        void context.executeJob(latestJob, 'scheduled', false, run.attempt + 1).catch((error) => {
            logger.warn('AutomationManager: retry execution failed', {
                jobId: latestJob.id,
                runId: run.id,
                attempt: run.attempt + 1,
                error: summarizeError(error),
            });
        });
    }, Math.max(1_000, job.failure.cooldownMs));
    context.retryTimers.set(retryKey, timer);
}
export function maybeDeliverAutomationFailureNotice(deliveryManager, job, run) {
    if (!deliveryManager)
        return;
    const routeIds = [job.failure.notifyRouteId, job.failure.deadLetterRouteId].filter((value) => typeof value === 'string' && value.length > 0);
    if (routeIds.length === 0)
        return;
    const targets = routeIds.map((routeId) => ({
        kind: 'surface',
        routeId,
    }));
    const message = [
        `Automation failure: ${job.name}`,
        `Run: ${run.id}`,
        `Status: ${run.status}`,
        run.error ? `Error: ${run.error}` : null,
        run.cancelledReason ? `Cancelled: ${run.cancelledReason}` : null,
    ].filter((value) => typeof value === 'string' && value.length > 0).join('\n');
    void deliveryManager.deliverText(job, run, message, targets).catch((error) => {
        logger.warn('AutomationManager: failure notice delivery failed', {
            jobId: job.id,
            runId: run.id,
            error: summarizeError(error),
        });
    });
}
export function maybeDeliverAutomationRun(context, job, run) {
    if (!context.deliveryManager)
        return;
    if (job.delivery.mode === 'none')
        return;
    if (context.deliveryInFlight.has(run.id))
        return;
    if (run.deliveryIds.length > 0)
        return;
    context.deliveryInFlight.add(run.id);
    void context.deliveryManager.deliverJobRun(job, run)
        .then(async (deliveryAttempts) => {
        if (deliveryAttempts.length === 0)
            return;
        const latest = context.runs.get(run.id);
        if (!latest)
            return;
        const updated = {
            ...latest,
            deliveryIds: deliveryAttempts.map((attempt) => attempt.id),
            deliveryAttempts,
            updatedAt: Date.now(),
        };
        context.runs.set(updated.id, updated);
        context.syncRunToRuntime(updated, 'automation.delivery');
        await context.saveRuns();
    })
        .catch((error) => {
        logger.warn('AutomationManager: delivery failed', {
            runId: run.id,
            jobId: job.id,
            error: summarizeError(error),
        });
    })
        .finally(() => {
        context.deliveryInFlight.delete(run.id);
    });
}
