/**
 * Listmonk EventBus Listener
 *
 * Subscribes to platform events and syncs subscribers to Listmonk newsletter lists.
 * Replaces the inline listmonkEventSyncService calls scattered across service files.
 *
 * Feature guard: LISTMONK_SYNC_ENABLED=true
 */

import { eventBus } from '../event-bus.service';
import { listmonkEventSyncService } from '../listmonk-event-sync.service';

export function registerListmonkListener(): void {
  // Shift signups → Volunteers list
  eventBus.subscribe('shift.signup.created', 'listmonk:shift-signup', (payload) => {
    listmonkEventSyncService.onShiftSignup({
      email: payload.userEmail,
      name: payload.userName,
      shiftTitle: payload.shiftTitle,
      shiftDate: payload.shiftDate,
      cutName: payload.cutName ?? undefined,
    });
  });

  // Canvass session completed → Canvassers list
  eventBus.subscribe('canvass.session.completed', 'listmonk:canvass-completed', (payload) => {
    listmonkEventSyncService.onCanvassSessionCompleted({
      email: payload.userEmail,
      name: payload.userName,
      cutName: payload.cutName,
      visitCount: payload.visitCount,
      outcomes: payload.outcomes,
    });
  });

  // Campaign email sent → Campaign Participants list
  eventBus.subscribe('campaign.email.sent', 'listmonk:campaign-email', (payload) => {
    listmonkEventSyncService.onCampaignEmailSent({
      email: payload.email,
      name: payload.name,
      campaignSlug: payload.campaignSlug,
      postalCode: payload.postalCode,
    });
  });

  // Address updated (canvass visit) → Support level lists
  eventBus.subscribe('contact.address.updated', 'listmonk:address-updated', (payload) => {
    listmonkEventSyncService.onAddressUpdated({
      email: payload.email,
      name: payload.name,
      supportLevel: payload.supportLevel,
      sign: payload.sign,
      address: payload.address,
    });
  });

  // Subscription activated → Subscribers list
  eventBus.subscribe('payment.subscription.activated', 'listmonk:subscription', (payload) => {
    listmonkEventSyncService.onSubscriptionActivated({
      email: payload.email,
      name: payload.name,
      planName: payload.planName,
      subscriptionId: payload.subscriptionId,
    });
  });

  // Donation completed → Donors list
  eventBus.subscribe('payment.donation.completed', 'listmonk:donation', (payload) => {
    listmonkEventSyncService.onDonationCompleted({
      email: payload.email,
      name: payload.name,
      amountCents: payload.amountCents,
      orderId: payload.orderId,
    });
  });

  // Product purchased → Donors list
  eventBus.subscribe('payment.product.purchased', 'listmonk:product-purchase', (payload) => {
    listmonkEventSyncService.onProductPurchased({
      email: payload.email,
      name: payload.name,
      productTitle: payload.productTitle,
      amountCents: payload.amountCents,
      orderId: payload.orderId,
    });
  });

  // Contact tags changed → CRM tag lists
  eventBus.subscribe('contact.tags.changed', 'listmonk:contact-tags', (payload) => {
    listmonkEventSyncService.onContactTagsChanged({
      email: payload.email,
      name: payload.name,
      addedTags: payload.addedTags,
      removedTags: payload.removedTags,
    });
  });

  // Reengagement sent → Volunteers list
  eventBus.subscribe('reengagement.sent', 'listmonk:reengagement', (payload) => {
    listmonkEventSyncService.onReengagementSent({
      email: payload.email,
      name: payload.name,
    });
  });
}
