/**
 * MCP tool: support.runIncidentPipeline
 *
 * Wraps the full A2A incident pipeline as a single callable MCP tool.
 * Agents run in sequence: Dispatcher → TelemetryAnalyst → DiagnosticsOrchestrator → ReportWriter.
 */

import { registerTool } from '../tools/registry.js';
import { SupportIncidentBriefSchema } from './types.js';
import { runIncidentPipeline } from './runner.js';
import type { McpToolRequest } from '../tools/types.js';

registerTool({
  name: 'support.runIncidentPipeline',
  description: [
    'A2A incident pipeline: Dispatcher → TelemetryAnalyst → DiagnosticsOrchestrator → ReportWriter.',
    'Provide a productId + userReport describing the issue. The pipeline queries telemetry clusters,',
    'generates hypotheses and recommended actions, optionally opens a live diagnostics session',
    '(set openDiagnosticsSession: true + userId/anonymousInstallId), and returns a FinalIncidentReport',
    'with a markdown summary. Every step is logged with a stable runId for audit tracing.',
    'Requires admin role.',
  ].join(' '),
  requiredRole: 'admin',
  inputSchema: SupportIncidentBriefSchema,
  async execute(args, req: McpToolRequest) {
    const token = req.headers.authorization?.slice(7);
    return runIncidentPipeline(args, {
      token,
      requestId: req.id,
      initiatedBy: req.jwtPayload?.sub,
      log: req.log,
    });
  },
});
