export interface ProcessorData {
  name: string
  type: string
  durationMs: number
  status: 'ok' | 'slow' | 'fail'
  startMs: number
}

export interface Exchange {
  id: string
  orderId: string
  customer: string
  route: string
  routeGroup: string
  status: 'completed' | 'failed' | 'running' | 'warning'
  durationMs: number
  timestamp: Date
  correlationId: string
  agent: string
  errorMessage?: string
  errorClass?: string
  processors: ProcessorData[]
  correlationGroup?: string
}

export const exchanges: Exchange[] = [
  {
    id: 'E-2026-03-18-00201',
    orderId: 'OP-92184',
    customer: 'CUST-44210',
    route: 'order-intake',
    routeGroup: 'order-flow',
    status: 'completed',
    durationMs: 87,
    timestamp: new Date('2026-03-18T09:12:04'),
    correlationId: 'cmr-f4a1c82b-9d3e',
    agent: 'prod-1',
    correlationGroup: 'order-flow-001',
    processors: [
      { name: 'from(jms:orders)', type: 'consumer', durationMs: 4, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 6, status: 'ok', startMs: 4 },
      { name: 'validate(schema)', type: 'process', durationMs: 10, status: 'ok', startMs: 10 },
      { name: 'enrich(customer-db)', type: 'enrich', durationMs: 52, status: 'ok', startMs: 20 },
      { name: 'to(order-enrichment)', type: 'to', durationMs: 15, status: 'ok', startMs: 72 },
    ],
  },
  {
    id: 'E-2026-03-18-00200',
    orderId: 'OP-92183',
    customer: 'CUST-18773',
    route: 'payment-validate',
    routeGroup: 'payment-flow',
    status: 'completed',
    durationMs: 164,
    timestamp: new Date('2026-03-18T09:11:22'),
    correlationId: 'cmr-7b2d9f14-c5a8',
    agent: 'prod-2',
    correlationGroup: 'payment-flow-001',
    processors: [
      { name: 'from(jms:payments)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 8, status: 'ok', startMs: 5 },
      { name: 'validate(payment-schema)', type: 'process', durationMs: 15, status: 'ok', startMs: 13 },
      { name: 'check(fraud-service)', type: 'enrich', durationMs: 112, status: 'ok', startMs: 28 },
      { name: 'to(payment-process)', type: 'to', durationMs: 24, status: 'ok', startMs: 140 },
    ],
  },
  {
    id: 'E-2026-03-18-00202',
    orderId: 'OP-92185',
    customer: 'CUST-67501',
    route: 'order-enrichment',
    routeGroup: 'order-flow',
    status: 'running',
    durationMs: 30000,
    timestamp: new Date('2026-03-18T09:13:44'),
    correlationId: 'cmr-3c8e1a7f-d2b6',
    agent: 'prod-1',
    correlationGroup: 'order-flow-001',
    processors: [
      { name: 'from(jms:orders)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'enrich(inventory-api)', type: 'enrich', durationMs: 29990, status: 'slow', startMs: 5 },
    ],
  },
  {
    id: 'E-2026-03-18-00198',
    orderId: 'OP-92180',
    customer: 'CUST-55019',
    route: 'shipment-dispatch',
    routeGroup: 'shipment-flow',
    status: 'warning',
    durationMs: 248,
    timestamp: new Date('2026-03-18T09:09:47'),
    correlationId: 'cmr-a9f3b2c1-e4d7',
    agent: 'prod-3',
    correlationGroup: 'shipment-flow-001',
    processors: [
      { name: 'from(jms:shipments)', type: 'consumer', durationMs: 6, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 8, status: 'ok', startMs: 6 },
      { name: 'enrich(carrier-api)', type: 'enrich', durationMs: 198, status: 'slow', startMs: 14 },
      { name: 'to(carrier-gateway)', type: 'to', durationMs: 36, status: 'ok', startMs: 212 },
    ],
  },
  {
    id: 'E-2026-03-18-00191',
    orderId: 'OP-88421',
    customer: 'CUST-31094',
    route: 'payment-process',
    routeGroup: 'payment-flow',
    status: 'failed',
    durationMs: 412,
    timestamp: new Date('2026-03-18T09:06:11'),
    correlationId: 'cmr-9a4f2b71-e8c3',
    agent: 'prod-2',
    correlationGroup: 'payment-flow-002',
    errorMessage: 'org.apache.camel.CamelExecutionException: Payment gateway timeout after 5000ms — POST https://pay.provider.com/v2/charge returned HTTP 504. Retry exhausted (3/3).',
    errorClass: 'org.apache.camel.CamelExecutionException',
    processors: [
      { name: 'from(jms:payments)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 8, status: 'ok', startMs: 5 },
      { name: 'validate(payment-schema)', type: 'process', durationMs: 12, status: 'ok', startMs: 13 },
      { name: 'to(payment-gateway)', type: 'to', durationMs: 387, status: 'fail', startMs: 25 },
    ],
  },
  {
    id: 'E-2026-03-18-00189',
    orderId: 'OP-88419',
    customer: 'CUST-72841',
    route: 'order-enrichment',
    routeGroup: 'order-flow',
    status: 'failed',
    durationMs: 1247,
    timestamp: new Date('2026-03-18T09:02:38'),
    correlationId: 'cmr-b6d7c3a2-f1e9',
    agent: 'prod-1',
    errorMessage: 'java.sql.SQLTransientConnectionException: HikariPool-1 - Connection not available, request timed out after 1000ms. (pool: 10, active: 10, idle: 0, waiting: 3)',
    errorClass: 'java.sql.SQLTransientConnectionException',
    processors: [
      { name: 'from(jms:orders)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 9, status: 'ok', startMs: 5 },
      { name: 'enrich(customer-db)', type: 'enrich', durationMs: 1233, status: 'fail', startMs: 14 },
    ],
  },
  {
    id: 'E-2026-03-18-00186',
    orderId: 'OP-92179',
    customer: 'CUST-90123',
    route: 'order-intake',
    routeGroup: 'order-flow',
    status: 'completed',
    durationMs: 62,
    timestamp: new Date('2026-03-18T09:00:15'),
    correlationId: 'cmr-2e5f8d9a-b4c1',
    agent: 'prod-3',
    correlationGroup: 'order-flow-001',
    processors: [
      { name: 'from(jms:orders)', type: 'consumer', durationMs: 3, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 5, status: 'ok', startMs: 3 },
      { name: 'validate(schema)', type: 'process', durationMs: 8, status: 'ok', startMs: 8 },
      { name: 'enrich(customer-db)', type: 'enrich', durationMs: 34, status: 'ok', startMs: 16 },
      { name: 'to(order-enrichment)', type: 'to', durationMs: 12, status: 'ok', startMs: 50 },
    ],
  },
  {
    id: 'E-2026-03-18-00184',
    orderId: 'OP-92178',
    customer: 'CUST-44210',
    route: 'payment-validate',
    routeGroup: 'payment-flow',
    status: 'completed',
    durationMs: 131,
    timestamp: new Date('2026-03-18T08:58:33'),
    correlationId: 'cmr-d1a3e7f4-c2b8',
    agent: 'prod-1',
    correlationGroup: 'payment-flow-001',
    processors: [
      { name: 'from(jms:payments)', type: 'consumer', durationMs: 4, status: 'ok', startMs: 0 },
      { name: 'validate(payment-schema)', type: 'process', durationMs: 14, status: 'ok', startMs: 4 },
      { name: 'check(fraud-service)', type: 'enrich', durationMs: 88, status: 'ok', startMs: 18 },
      { name: 'to(payment-process)', type: 'to', durationMs: 25, status: 'ok', startMs: 106 },
    ],
  },
  {
    id: 'E-2026-03-18-00179',
    orderId: 'OP-92175',
    customer: 'CUST-67892',
    route: 'shipment-track',
    routeGroup: 'shipment-flow',
    status: 'completed',
    durationMs: 94,
    timestamp: new Date('2026-03-18T08:53:07'),
    correlationId: 'cmr-e8b2c9d4-a7f3',
    agent: 'prod-4',
    processors: [
      { name: 'from(jms:tracking)', type: 'consumer', durationMs: 4, status: 'ok', startMs: 0 },
      { name: 'enrich(carrier-api)', type: 'enrich', durationMs: 72, status: 'ok', startMs: 4 },
      { name: 'to(notification-hub)', type: 'to', durationMs: 18, status: 'ok', startMs: 76 },
    ],
  },
  {
    id: 'E-2026-03-18-00177',
    orderId: 'OP-92174',
    customer: 'CUST-15293',
    route: 'order-intake',
    routeGroup: 'order-flow',
    status: 'completed',
    durationMs: 73,
    timestamp: new Date('2026-03-18T08:50:41'),
    correlationId: 'cmr-f3c7a1b9-d5e2',
    agent: 'prod-1',
    correlationGroup: 'order-flow-001',
    processors: [
      { name: 'from(jms:orders)', type: 'consumer', durationMs: 3, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 6, status: 'ok', startMs: 3 },
      { name: 'validate(schema)', type: 'process', durationMs: 9, status: 'ok', startMs: 9 },
      { name: 'enrich(customer-db)', type: 'enrich', durationMs: 40, status: 'ok', startMs: 18 },
      { name: 'to(order-enrichment)', type: 'to', durationMs: 15, status: 'ok', startMs: 58 },
    ],
  },
  {
    id: 'E-2026-03-18-00173',
    orderId: 'OP-92173',
    customer: 'CUST-88104',
    route: 'payment-process',
    routeGroup: 'payment-flow',
    status: 'completed',
    durationMs: 198,
    timestamp: new Date('2026-03-18T08:46:19'),
    correlationId: 'cmr-a2d8f5c3-b9e1',
    agent: 'prod-2',
    correlationGroup: 'payment-flow-001',
    processors: [
      { name: 'from(jms:payments)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'validate(payment-schema)', type: 'process', durationMs: 12, status: 'ok', startMs: 5 },
      { name: 'to(payment-gateway)', type: 'to', durationMs: 156, status: 'ok', startMs: 17 },
      { name: 'to(order-service)', type: 'to', durationMs: 25, status: 'ok', startMs: 173 },
    ],
  },
  {
    id: 'E-2026-03-18-00164',
    orderId: 'OP-92170',
    customer: 'CUST-50321',
    route: 'order-enrichment',
    routeGroup: 'order-flow',
    status: 'warning',
    durationMs: 267,
    timestamp: new Date('2026-03-18T08:39:52'),
    correlationId: 'cmr-c4f1b7a8-e3d6',
    agent: 'prod-1',
    processors: [
      { name: 'from(jms:orders)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 8, status: 'ok', startMs: 5 },
      { name: 'enrich(inventory-api)', type: 'enrich', durationMs: 224, status: 'slow', startMs: 13 },
      { name: 'to(payment-process)', type: 'to', durationMs: 30, status: 'ok', startMs: 237 },
    ],
  },
  {
    id: 'E-2026-03-18-00158',
    orderId: 'OP-88412',
    customer: 'CUST-23781',
    route: 'payment-process',
    routeGroup: 'payment-flow',
    status: 'failed',
    durationMs: 5234,
    timestamp: new Date('2026-03-18T08:31:05'),
    correlationId: 'cmr-7e9a2c5f-d1b4',
    agent: 'prod-2',
    correlationGroup: 'payment-flow-002',
    errorMessage: 'org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking https://pay.provider.com/v2/charge with statusCode: 422 — Unprocessable Entity: card declined (insufficient funds)',
    errorClass: 'org.apache.camel.component.http.HttpOperationFailedException',
    processors: [
      { name: 'from(jms:payments)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'validate(payment-schema)', type: 'process', durationMs: 11, status: 'ok', startMs: 5 },
      { name: 'to(payment-gateway)', type: 'to', durationMs: 5218, status: 'fail', startMs: 16 },
    ],
  },
  {
    id: 'E-2026-03-18-00151',
    orderId: 'OP-92166',
    customer: 'CUST-61042',
    route: 'shipment-dispatch',
    routeGroup: 'shipment-flow',
    status: 'completed',
    durationMs: 112,
    timestamp: new Date('2026-03-18T08:22:44'),
    correlationId: 'cmr-b5c8d2a7-f4e3',
    agent: 'prod-3',
    correlationGroup: 'shipment-flow-001',
    processors: [
      { name: 'from(jms:shipments)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 7, status: 'ok', startMs: 5 },
      { name: 'enrich(carrier-api)', type: 'enrich', durationMs: 80, status: 'ok', startMs: 12 },
      { name: 'to(carrier-gateway)', type: 'to', durationMs: 20, status: 'ok', startMs: 92 },
    ],
  },
  {
    id: 'E-2026-03-18-00144',
    orderId: 'OP-92161',
    customer: 'CUST-77215',
    route: 'order-intake',
    routeGroup: 'order-flow',
    status: 'completed',
    durationMs: 91,
    timestamp: new Date('2026-03-18T08:15:19'),
    correlationId: 'cmr-d9e3f7b1-a6c5',
    agent: 'prod-4',
    correlationGroup: 'order-flow-001',
    processors: [
      { name: 'from(jms:orders)', type: 'consumer', durationMs: 4, status: 'ok', startMs: 0 },
      { name: 'unmarshal(json)', type: 'transform', durationMs: 7, status: 'ok', startMs: 4 },
      { name: 'validate(schema)', type: 'process', durationMs: 10, status: 'ok', startMs: 11 },
      { name: 'enrich(customer-db)', type: 'enrich', durationMs: 54, status: 'ok', startMs: 21 },
      { name: 'to(order-enrichment)', type: 'to', durationMs: 16, status: 'ok', startMs: 75 },
    ],
  },
]

// ── Generate additional exchanges to reach ~200 total ────────────────────────

const GEN_ROUTES = [
  'order-intake', 'payment-validate', 'order-enrichment', 'shipment-dispatch',
  'payment-process', 'shipment-track', 'inventory-sync', 'notification-send',
  'audit-log', 'customer-update', 'refund-process', 'invoice-generate',
]
const GEN_ROUTE_GROUPS = [
  'order-flow', 'payment-flow', 'shipment-flow', 'inventory-flow',
  'notification-flow', 'audit-flow', 'customer-flow', 'billing-flow',
]
const GEN_AGENTS = ['prod-1', 'prod-2', 'prod-3', 'prod-4']
const GEN_STATUSES: Exchange['status'][] = [
  'completed', 'completed', 'completed', 'completed', 'completed',
  'completed', 'failed', 'warning', 'running',
]
const GEN_PROCESSORS: ProcessorData[][] = [
  [
    { name: 'from(jms:queue)', type: 'consumer', durationMs: 4, status: 'ok', startMs: 0 },
    { name: 'unmarshal(json)', type: 'transform', durationMs: 6, status: 'ok', startMs: 4 },
    { name: 'validate(schema)', type: 'process', durationMs: 10, status: 'ok', startMs: 10 },
    { name: 'to(target)', type: 'to', durationMs: 15, status: 'ok', startMs: 20 },
  ],
  [
    { name: 'from(jms:queue)', type: 'consumer', durationMs: 5, status: 'ok', startMs: 0 },
    { name: 'enrich(external-api)', type: 'enrich', durationMs: 120, status: 'slow', startMs: 5 },
    { name: 'to(target)', type: 'to', durationMs: 20, status: 'ok', startMs: 125 },
  ],
  [
    { name: 'from(jms:queue)', type: 'consumer', durationMs: 3, status: 'ok', startMs: 0 },
    { name: 'to(external-gateway)', type: 'to', durationMs: 400, status: 'fail', startMs: 3 },
  ],
]
const GEN_ERRORS = [
  { msg: 'org.apache.camel.CamelExecutionException: Timeout after 5000ms', cls: 'org.apache.camel.CamelExecutionException' },
  { msg: 'java.sql.SQLTransientConnectionException: Connection pool exhausted', cls: 'java.sql.SQLTransientConnectionException' },
  { msg: 'org.apache.camel.component.http.HttpOperationFailedException: HTTP 502 Bad Gateway', cls: 'org.apache.camel.component.http.HttpOperationFailedException' },
]

const BASE_TIME = new Date('2026-03-18T08:00:00').getTime()

for (let i = 0; i < 185; i++) {
  const idx = exchanges.length
  const status = GEN_STATUSES[i % GEN_STATUSES.length]
  const route = GEN_ROUTES[i % GEN_ROUTES.length]
  const routeGroup = GEN_ROUTE_GROUPS[i % GEN_ROUTE_GROUPS.length]
  const isFailed = status === 'failed'
  const durationMs = isFailed
    ? 1000 + ((i * 37) % 4000)
    : status === 'running'
      ? 10000 + ((i * 53) % 20000)
      : 30 + ((i * 73) % 400)
  const err = isFailed ? GEN_ERRORS[i % GEN_ERRORS.length] : undefined

  exchanges.push({
    id: `E-2026-03-18-${String(idx + 200).padStart(5, '0')}`,
    orderId: `OP-${80000 + idx}`,
    customer: `CUST-${10000 + ((i * 131) % 90000)}`,
    route,
    routeGroup,
    status,
    durationMs,
    timestamp: new Date(BASE_TIME - i * 2 * 60 * 1000),
    correlationId: `cmr-${i.toString(16).padStart(8, '0')}-gen`,
    agent: GEN_AGENTS[i % GEN_AGENTS.length],
    ...(err ? { errorMessage: err.msg, errorClass: err.cls } : {}),
    processors: GEN_PROCESSORS[i % GEN_PROCESSORS.length].map((p) => ({ ...p })),
    ...(i % 3 === 0 ? { correlationGroup: `${routeGroup}-${String(Math.floor(i / 3)).padStart(3, '0')}` } : {}),
  })
}
