import { endTaskSpan, recordTaskPhase, startTaskSpan } from '@pellux/goodvibes-sdk/platform/runtime/telemetry/spans/task';
import { endTransportSpan, recordTransportPhase, startTransportSpan } from '@pellux/goodvibes-sdk/platform/runtime/telemetry/spans/transport';
export function attachTransportDomain({ bus, helpers }, transportSpans) {
    const unsubs = [];
    unsubs.push(bus.on('TRANSPORT_INITIALIZING', (env) => {
        helpers.safe(() => {
            const span = startTransportSpan(helpers.tracer, {
                transportId: env.payload.transportId,
                protocol: env.payload.protocol,
                traceId: env.traceId,
            });
            transportSpans.set(env.payload.transportId, span);
        });
    }));
    unsubs.push(bus.on('TRANSPORT_AUTHENTICATING', (env) => {
        helpers.safe(() => {
            helpers.withSpan(transportSpans, env.payload.transportId, (span) => recordTransportPhase(span, 'authenticating'));
        });
    }));
    unsubs.push(bus.on('TRANSPORT_CONNECTED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(transportSpans, env.payload.transportId, (span) => {
                recordTransportPhase(span, 'connected', {
                    'transport.endpoint': env.payload.endpoint,
                });
            });
        });
    }));
    unsubs.push(bus.on('TRANSPORT_SYNCING', (env) => {
        helpers.safe(() => {
            helpers.withSpan(transportSpans, env.payload.transportId, (span) => recordTransportPhase(span, 'syncing'));
        });
    }));
    unsubs.push(bus.on('TRANSPORT_DEGRADED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(transportSpans, env.payload.transportId, (span) => {
                recordTransportPhase(span, 'degraded', {
                    'transport.degraded_reason': env.payload.reason,
                });
            });
        });
    }));
    unsubs.push(bus.on('TRANSPORT_RECONNECTING', (env) => {
        helpers.safe(() => {
            helpers.withSpan(transportSpans, env.payload.transportId, (span) => {
                recordTransportPhase(span, 'reconnecting', {
                    'transport.reconnect_attempt': env.payload.attempt,
                    'transport.reconnect_max_attempts': env.payload.maxAttempts,
                });
            });
        });
    }));
    unsubs.push(bus.on('TRANSPORT_DISCONNECTED', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(transportSpans, env.payload.transportId, (span) => {
                endTransportSpan(span, {
                    outcome: 'disconnected',
                    reason: env.payload.reason,
                    willRetry: env.payload.willRetry,
                });
            });
        });
    }));
    unsubs.push(bus.on('TRANSPORT_TERMINAL_FAILURE', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(transportSpans, env.payload.transportId, (span) => {
                endTransportSpan(span, {
                    outcome: 'terminal_failure',
                    reason: env.payload.error,
                });
            });
        });
    }));
    return () => unsubs.forEach((unsub) => unsub());
}
export function attachTaskDomain({ bus, helpers }, taskSpans) {
    const unsubs = [];
    unsubs.push(bus.on('TASK_CREATED', (env) => {
        helpers.safe(() => {
            const span = startTaskSpan(helpers.tracer, {
                taskId: env.payload.taskId,
                agentId: env.payload.agentId,
                description: env.payload.description,
                priority: env.payload.priority,
                traceId: env.traceId,
            });
            taskSpans.set(env.payload.taskId, span);
        });
    }));
    unsubs.push(bus.on('TASK_STARTED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(taskSpans, env.payload.taskId, (span) => recordTaskPhase(span, 'started'));
        });
    }));
    unsubs.push(bus.on('TASK_BLOCKED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(taskSpans, env.payload.taskId, (span) => {
                recordTaskPhase(span, 'blocked', {
                    'task.blocked_reason': env.payload.reason,
                });
            });
        });
    }));
    unsubs.push(bus.on('TASK_PROGRESS', (env) => {
        helpers.safe(() => {
            helpers.withSpan(taskSpans, env.payload.taskId, (span) => {
                recordTaskPhase(span, 'progress', {
                    'task.progress': env.payload.progress,
                });
            });
        });
    }));
    unsubs.push(bus.on('TASK_COMPLETED', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(taskSpans, env.payload.taskId, (span) => {
                endTaskSpan(span, {
                    outcome: 'completed',
                    durationMs: env.payload.durationMs,
                });
            });
        });
    }));
    unsubs.push(bus.on('TASK_FAILED', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(taskSpans, env.payload.taskId, (span) => {
                endTaskSpan(span, {
                    outcome: 'failed',
                    durationMs: env.payload.durationMs,
                    error: env.payload.error,
                });
            });
        });
    }));
    unsubs.push(bus.on('TASK_CANCELLED', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(taskSpans, env.payload.taskId, (span) => {
                endTaskSpan(span, {
                    outcome: 'cancelled',
                    durationMs: 0,
                    reason: env.payload.reason,
                });
            });
        });
    }));
    return () => unsubs.forEach((unsub) => unsub());
}
