import { eventBus } from '../bus';
import type { FactoryEvent } from '../types';
import { createNotification } from '../../services/notification.service';

/**
 * Notification Worker Consumer
 * Listens for events that should trigger user notifications
 */
export function startNotificationWorker(): void {
  // Task created → notify assignee
  eventBus.on('task.created', async (event: FactoryEvent) => {
    const { taskId, title, priority } = event.data as any;
    // In production, look up the assignee and notify
    console.log(`[Notification] Task created: ${title} (${priority})`);
  });

  // SLA warning → urgent notification
  eventBus.on('task.sla_warning', async (event: FactoryEvent) => {
    const { taskId, title, minutesRemaining } = event.data as any;
    console.log(`[Notification] SLA Warning: ${title} — ${minutesRemaining}min remaining`);
  });

  // SLA breach → critical notification
  eventBus.on('task.sla_breached', async (event: FactoryEvent) => {
    const { taskId, title } = event.data as any;
    console.log(`[Notification] 🚨 SLA BREACH: ${title}`);
  });

  // Approval resolved → notify relevant parties
  eventBus.on('approval.approved', async (event: FactoryEvent) => {
    const { approvalId, pipelineId, stageName } = event.data as any;
    console.log(`[Notification] Approval approved: ${stageName} for pipeline ${pipelineId}`);
  });

  eventBus.on('approval.rejected', async (event: FactoryEvent) => {
    const { approvalId, pipelineId, stageName, reason } = event.data as any;
    console.log(`[Notification] Approval rejected: ${stageName} — ${reason}`);
  });

  console.log('[Consumer:Notification] Worker started');
}
