import { endMcpSpan, recordMcpPhase, startMcpSpan } from '../spans/mcp.js';
import { endPluginSpan, recordPluginPhase, startPluginSpan } from '../spans/plugin.js';
export function attachPluginDomain({ bus, helpers }, pluginSpans) {
    const unsubs = [];
    unsubs.push(bus.on('PLUGIN_DISCOVERED', (env) => {
        helpers.safe(() => {
            const span = startPluginSpan(helpers.tracer, {
                pluginId: env.payload.pluginId,
                path: env.payload.path,
                version: env.payload.version,
                traceId: env.traceId ?? '',
            });
            pluginSpans.set(env.payload.pluginId, span);
        });
    }));
    unsubs.push(bus.on('PLUGIN_LOADING', (env) => {
        helpers.safe(() => {
            helpers.withSpan(pluginSpans, env.payload.pluginId, (span) => recordPluginPhase(span, 'loading'));
        });
    }));
    unsubs.push(bus.on('PLUGIN_LOADED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(pluginSpans, env.payload.pluginId, (span) => {
                recordPluginPhase(span, 'loaded', {
                    'plugin.capability_count': env.payload.capabilities.length,
                });
            });
        });
    }));
    unsubs.push(bus.on('PLUGIN_ACTIVE', (env) => {
        helpers.safe(() => {
            helpers.withSpan(pluginSpans, env.payload.pluginId, (span) => recordPluginPhase(span, 'active'));
        });
    }));
    unsubs.push(bus.on('PLUGIN_DEGRADED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(pluginSpans, env.payload.pluginId, (span) => {
                recordPluginPhase(span, 'degraded', {
                    'plugin.degraded_reason': env.payload.reason,
                    'plugin.affected_capability_count': env.payload.affectedCapabilities.length,
                });
            });
        });
    }));
    unsubs.push(bus.on('PLUGIN_ERROR', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(pluginSpans, env.payload.pluginId, (span) => {
                endPluginSpan(span, {
                    outcome: 'error',
                    error: env.payload.error,
                });
            });
        });
    }));
    unsubs.push(bus.on('PLUGIN_UNLOADING', (env) => {
        helpers.safe(() => {
            helpers.withSpan(pluginSpans, env.payload.pluginId, (span) => recordPluginPhase(span, 'unloading'));
        });
    }));
    unsubs.push(bus.on('PLUGIN_DISABLED', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(pluginSpans, env.payload.pluginId, (span) => {
                endPluginSpan(span, {
                    outcome: 'disabled',
                    reason: env.payload.reason,
                });
            });
        });
    }));
    return () => unsubs.forEach((unsub) => unsub());
}
export function attachMcpDomain({ bus, helpers }, mcpSpans) {
    const unsubs = [];
    unsubs.push(bus.on('MCP_CONFIGURED', (env) => {
        helpers.safe(() => {
            const span = startMcpSpan(helpers.tracer, {
                serverId: env.payload.serverId,
                transport: env.payload.transport,
                url: env.payload.url,
                traceId: env.traceId ?? '',
            });
            mcpSpans.set(env.payload.serverId, span);
        });
    }));
    unsubs.push(bus.on('MCP_CONNECTING', (env) => {
        helpers.safe(() => {
            helpers.withSpan(mcpSpans, env.payload.serverId, (span) => recordMcpPhase(span, 'connecting'));
        });
    }));
    unsubs.push(bus.on('MCP_CONNECTED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(mcpSpans, env.payload.serverId, (span) => {
                recordMcpPhase(span, 'connected', {
                    'mcp.tool_count': env.payload.toolCount,
                    'mcp.resource_count': env.payload.resourceCount,
                });
            });
        });
    }));
    unsubs.push(bus.on('MCP_DEGRADED', (env) => {
        helpers.safe(() => {
            helpers.withSpan(mcpSpans, env.payload.serverId, (span) => {
                recordMcpPhase(span, 'degraded', {
                    'mcp.degraded_reason': env.payload.reason,
                    'mcp.available_tool_count': env.payload.availableTools.length,
                });
            });
        });
    }));
    unsubs.push(bus.on('MCP_AUTH_REQUIRED', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(mcpSpans, env.payload.serverId, (span) => {
                recordMcpPhase(span, 'auth_required', {
                    'mcp.auth_type': env.payload.authType,
                });
                endMcpSpan(span, { outcome: 'auth_failed' });
            });
        });
    }));
    unsubs.push(bus.on('MCP_RECONNECTING', (env) => {
        helpers.safe(() => {
            helpers.withSpan(mcpSpans, env.payload.serverId, (span) => {
                recordMcpPhase(span, 'reconnecting', {
                    'mcp.reconnect_attempt': env.payload.attempt,
                    'mcp.reconnect_max_attempts': env.payload.maxAttempts,
                });
            });
        });
    }));
    unsubs.push(bus.on('MCP_DISCONNECTED', (env) => {
        helpers.safe(() => {
            helpers.closeSpan(mcpSpans, env.payload.serverId, (span) => {
                endMcpSpan(span, {
                    outcome: 'disconnected',
                    reason: env.payload.reason,
                    willRetry: env.payload.willRetry,
                });
            });
        });
    }));
    return () => unsubs.forEach((unsub) => unsub());
}
