// ═══════════════════════════════════════════════════════════
// Event Type Definitions — Redis Streams Event Bus
// ═══════════════════════════════════════════════════════════

export type EventType =
  // Pipeline events
  | 'pipeline.created'
  | 'pipeline.updated'
  | 'pipeline.stage_changed'
  | 'pipeline.blocked'
  | 'pipeline.completed'
  | 'pipeline.failed'
  // Task events
  | 'task.created'
  | 'task.claimed'
  | 'task.completed'
  | 'task.sla_warning'
  | 'task.sla_breached'
  | 'task.escalated'
  // Approval events
  | 'approval.pending'
  | 'approval.approved'
  | 'approval.rejected'
  | 'approval.escalated'
  // Agent events
  | 'agent.status_changed'
  | 'agent.heartbeat'
  | 'agent.error'
  // Asset events
  | 'asset.created'
  | 'asset.updated'
  // Deploy events
  | 'deploy.started'
  | 'deploy.succeeded'
  | 'deploy.failed'
  // Test events
  | 'test.started'
  | 'test.completed'
  | 'test.failed';

export interface FactoryEvent<T = Record<string, unknown>> {
  id?: string;
  type: EventType;
  timestamp: string;
  data: T;
  actor?: {
    type: 'user' | 'agent' | 'system';
    id: string;
    name?: string;
  };
}

// ═══════════════════════════════════════════════════════════
// Event Data Payloads
// ═══════════════════════════════════════════════════════════

export interface PipelineCreatedData {
  pipelineId: string;
  name: string;
  template: string;
  priority: string;
}

export interface PipelineStageChangedData {
  pipelineId: string;
  pipelineName: string;
  fromStage: string;
  toStage: string;
  triggeredBy: string;
}

export interface TaskCreatedData {
  taskId: string;
  pipelineId: string | null;
  type: string;
  title: string;
  priority: string;
  slaDeadline: string | null;
  blocksStageAdvance: boolean;
}

export interface TaskCompletedData {
  taskId: string;
  pipelineId: string | null;
  decision: string;
  decidedBy: string;
  notes: string | null;
}

export interface ApprovalResolvedData {
  approvalId: string;
  taskId: string | null;
  pipelineId: string | null;
  stageName: string;
  status: 'approved' | 'rejected';
  resolvedBy: string;
  notes?: string;
  reason?: string;
}

export interface AgentStatusData {
  agentId: string;
  name: string;
  previousStatus: string;
  newStatus: string;
}

export interface SLAWarningData {
  taskId: string;
  pipelineId: string | null;
  title: string;
  deadline: string;
  minutesRemaining: number;
  escalationLevel: number;
}

// ═══════════════════════════════════════════════════════════
// Stream Names
// ═══════════════════════════════════════════════════════════

export const STREAMS = {
  PIPELINES: 'factory:pipelines',
  TASKS: 'factory:tasks',
  APPROVALS: 'factory:approvals',
  AGENTS: 'factory:agents',
  DEPLOYS: 'factory:deploys',
  NOTIFICATIONS: 'factory:notifications',
} as const;

export const CONSUMER_GROUPS = {
  WS_BROADCASTER: 'ws-broadcaster',
  NOTIFICATION_WORKER: 'notification-worker',
  DISCORD_BRIDGE: 'discord-bridge',
  SLA_MONITOR: 'sla-monitor',
  AUDIT_WRITER: 'audit-writer',
  MCP_RESOURCE_UPDATER: 'mcp-resource-updater',
} as const;
