/**
 * Rocket.Chat EventBus Listener
 *
 * Subscribes to platform events and posts notifications to RC channels.
 * Extends the existing rocketchat-webhook.service with new event coverage.
 *
 * Channels:
 *   #shifts     — shift CRUD + signups
 *   #canvassing — canvass sessions + visit milestones
 *   #campaigns  — campaign publish, responses, email milestones
 *
 * Feature guard: ENABLE_CHAT=true (checked inside rocketchatWebhookService)
 */

import { eventBus } from '../event-bus.service';
import { rocketchatWebhookService } from '../rocketchat-webhook.service';

export function registerRocketChatListener(): void {
  // --- Shifts ---

  eventBus.subscribe('shift.signup.created', 'rocketchat:shift-signup', (payload) => {
    rocketchatWebhookService.onShiftSignup({
      userName: payload.userName,
      shiftTitle: payload.shiftTitle,
      shiftDate: payload.shiftDate,
    });
  });

  eventBus.subscribe('shift.signup.cancelled', 'rocketchat:shift-cancel', (payload) => {
    rocketchatWebhookService.onShiftCancellation({
      userName: payload.userName,
      shiftTitle: payload.shiftTitle,
      shiftDate: payload.shiftDate,
    });
  });

  // --- Canvass ---

  eventBus.subscribe('canvass.session.completed', 'rocketchat:canvass-completed', (payload) => {
    rocketchatWebhookService.onCanvassSessionCompleted({
      userName: payload.userName,
      visitCount: payload.visitCount,
      cutName: payload.cutName,
    });
  });

  // --- Responses ---

  eventBus.subscribe('response.submitted', 'rocketchat:response-submitted', (payload) => {
    rocketchatWebhookService.onCampaignResponseSubmitted({
      campaignTitle: payload.campaignTitle,
      representativeName: payload.representativeName,
    });
  });

  // --- Campaigns ---

  eventBus.subscribe('campaign.published', 'rocketchat:campaign-published', (payload) => {
    rocketchatWebhookService.onCampaignPublished({
      campaignTitle: payload.title,
      campaignSlug: payload.slug,
    });
  });

  // --- Payments ---

  eventBus.subscribe('payment.donation.completed', 'rocketchat:donation', (payload) => {
    rocketchatWebhookService.onDonationReceived({
      donorName: payload.name || payload.email,
      amount: (payload.amountCents / 100).toFixed(2),
    });
  });

  eventBus.subscribe('payment.subscription.activated', 'rocketchat:subscription', (payload) => {
    rocketchatWebhookService.onSubscriptionActivated({
      userName: payload.name || payload.email,
      planName: payload.planName,
    });
  });

  // --- SMS escalations (QUESTION/NEGATIVE responses) ---

  eventBus.subscribe('sms.message.received', 'rocketchat:sms-escalation', (payload) => {
    if (payload.responseType === 'QUESTION' || payload.responseType === 'NEGATIVE') {
      rocketchatWebhookService.onSmsEscalation({
        phone: payload.phone,
        responseType: payload.responseType,
        body: payload.body,
      });
    }
  });

  // --- Users ---

  eventBus.subscribe('user.approved', 'rocketchat:user-approved', (payload) => {
    rocketchatWebhookService.onUserApproved({
      userName: payload.name,
      role: payload.role,
    });
  });

  // --- Media ---

  eventBus.subscribe('media.video.published', 'rocketchat:video-published', (payload) => {
    rocketchatWebhookService.onVideoPublished({
      videoTitle: payload.title,
    });
  });

  // --- Ticketed Events ---

  eventBus.subscribe('ticketed-event.published', 'rocketchat:ticketed-event', (payload) => {
    rocketchatWebhookService.onTicketedEventPublished({
      eventTitle: payload.title,
      eventDate: payload.date,
    });
  });
}
