/**
 * Automated credential issuance notifications
 */

import { getNotificationService, CredentialNotificationTemplates } from '@the-order/notifications';
import { getEventBus, CredentialEvents } from '@the-order/events';

/**
 * Initialize credential issuance notifications
 */
export async function initializeCredentialNotifications(): Promise<void> {
  const eventBus = getEventBus();
  const notificationService = getNotificationService();

  // Subscribe to credential issued events
  await eventBus.subscribe(CredentialEvents.ISSUED, async (data) => {
    const eventData = data as {
      subjectDid: string;
      credentialType: string | string[];
      credentialId?: string;
      issuedAt: string;
      recipientEmail?: string;
      recipientPhone?: string;
      recipientName?: string;
    };

    const credentialType = Array.isArray(eventData.credentialType)
      ? eventData.credentialType.join(', ')
      : eventData.credentialType;

    // Send email notification
    if (eventData.recipientEmail) {
      await notificationService.send({
        to: eventData.recipientEmail,
        type: 'email',
        subject: CredentialNotificationTemplates.ISSUED.email.subject,
        message: '', // Message will be generated from template
        template: CredentialNotificationTemplates.ISSUED.email.template,
        templateData: {
          recipientName: eventData.recipientName || 'User',
          credentialType,
          issuerName: 'The Order',
          issuedAt: eventData.issuedAt,
          credentialId: eventData.credentialId || 'N/A',
          credentialsUrl: process.env.CREDENTIALS_URL || 'https://theorder.org/credentials',
        },
      });
    }

    // Send SMS notification
    if (eventData.recipientPhone) {
      await notificationService.send({
        to: eventData.recipientPhone,
        type: 'sms',
        message: '', // Message will be generated from template
        template: CredentialNotificationTemplates.ISSUED.sms.template,
        templateData: {
          credentialType,
          credentialId: eventData.credentialId || 'N/A',
          credentialsUrl: process.env.CREDENTIALS_URL || 'https://theorder.org/credentials',
        },
      });
    }
  });

  // Subscribe to credential renewed events
  await eventBus.subscribe(CredentialEvents.RENEWED, async (data) => {
    const eventData = data as {
      subjectDid: string;
      oldCredentialId: string;
      newCredentialId: string;
      renewedAt: string;
      recipientEmail?: string;
      recipientPhone?: string;
      recipientName?: string;
    };

    if (eventData.recipientEmail) {
      await notificationService.send({
        to: eventData.recipientEmail,
        type: 'email',
        subject: CredentialNotificationTemplates.RENEWED.email.subject,
        message: '', // Message will be generated from template
        template: CredentialNotificationTemplates.RENEWED.email.template,
        templateData: {
          recipientName: eventData.recipientName || 'User',
          credentialType: 'Verifiable Credential',
          newCredentialId: eventData.newCredentialId,
          oldCredentialId: eventData.oldCredentialId,
          renewedAt: eventData.renewedAt,
          credentialsUrl: process.env.CREDENTIALS_URL || 'https://theorder.org/credentials',
        },
      });
    }
  });

  // Subscribe to credential expiring events
  await eventBus.subscribe(CredentialEvents.EXPIRING, async (data) => {
    const eventData = data as {
      credentialId: string;
      subjectDid: string;
      expirationDate: string;
      daysUntilExpiration: number;
      recipientEmail?: string;
      recipientPhone?: string;
      recipientName?: string;
    };

    if (eventData.recipientEmail) {
      await notificationService.send({
        to: eventData.recipientEmail,
        type: 'email',
        subject: CredentialNotificationTemplates.EXPIRING.email.subject,
        message: '', // Message will be generated from template
        template: CredentialNotificationTemplates.EXPIRING.email.template,
        templateData: {
          recipientName: eventData.recipientName || 'User',
          credentialType: 'Verifiable Credential',
          credentialId: eventData.credentialId,
          expirationDate: eventData.expirationDate,
          daysUntilExpiration: eventData.daysUntilExpiration,
          renewalUrl: process.env.RENEWAL_URL || 'https://theorder.org/credentials/renew',
        },
      });
    }

    if (eventData.recipientPhone) {
      await notificationService.send({
        to: eventData.recipientPhone,
        type: 'sms',
        message: '', // Message will be generated from template
        template: CredentialNotificationTemplates.EXPIRING.sms.template,
        templateData: {
          credentialType: 'Verifiable Credential',
          daysUntilExpiration: eventData.daysUntilExpiration,
          renewalUrl: process.env.RENEWAL_URL || 'https://theorder.org/credentials/renew',
        },
      });
    }
  });

  // Subscribe to credential revoked events
  await eventBus.subscribe(CredentialEvents.REVOKED, async (data) => {
    const eventData = data as {
      credentialId: string;
      subjectDid: string;
      reason: string;
      revokedAt: string;
      recipientEmail?: string;
      recipientPhone?: string;
      recipientName?: string;
    };

    if (eventData.recipientEmail) {
      await notificationService.send({
        to: eventData.recipientEmail,
        type: 'email',
        subject: CredentialNotificationTemplates.REVOKED.email.subject,
        message: '', // Message will be generated from template
        template: CredentialNotificationTemplates.REVOKED.email.template,
        templateData: {
          recipientName: eventData.recipientName || 'User',
          credentialType: 'Verifiable Credential',
          credentialId: eventData.credentialId,
          revokedAt: eventData.revokedAt,
          revocationReason: eventData.reason,
        },
      });
    }
  });
}

