/**
 * Notifications Package Tests
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { NotificationService, getNotificationService, CredentialNotificationTemplates } from './index';
import { getEnv } from '@the-order/shared';

vi.mock('@the-order/shared');

describe('NotificationService', () => {
  let service: NotificationService;

  beforeEach(() => {
    vi.clearAllMocks();
    (getEnv as any).mockReturnValue({
      EMAIL_PROVIDER: 'smtp',
      EMAIL_API_KEY: 'test-key',
      EMAIL_FROM: 'test@example.com',
      EMAIL_FROM_NAME: 'Test',
      SMS_PROVIDER: 'twilio',
      SMS_API_KEY: 'test-key',
      SMS_FROM_NUMBER: '+1234567890',
      PUSH_PROVIDER: 'fcm',
      PUSH_API_KEY: 'test-key',
    });
    service = new NotificationService();
  });

  describe('send', () => {
    it('should send email notification', async () => {
      const notification = {
        to: 'test@example.com',
        subject: 'Test Subject',
        message: 'Test Message',
        type: 'email' as const,
      };

      const result = await service.send(notification);

      expect(result.success).toBe(true);
      expect(result.messageId).toBeDefined();
    });

    it('should send SMS notification', async () => {
      const notification = {
        to: '+1234567890',
        message: 'Test Message',
        type: 'sms' as const,
      };

      const result = await service.send(notification);

      expect(result.success).toBe(true);
      expect(result.messageId).toBeDefined();
    });

    it('should send push notification', async () => {
      const notification = {
        to: 'test-device-token',
        message: 'Test Message',
        type: 'push' as const,
      };

      const result = await service.send(notification);

      expect(result.success).toBe(true);
      expect(result.messageId).toBeDefined();
    });

    it('should return error for unsupported notification type', async () => {
      const notification = {
        to: 'test@example.com',
        message: 'Test Message',
        type: 'unknown' as any,
      };

      const result = await service.send(notification);

      expect(result.success).toBe(false);
      expect(result.error).toContain('Unsupported notification type');
    });
  });

  describe('getNotificationService', () => {
    it('should return singleton instance', () => {
      const service1 = getNotificationService();
      const service2 = getNotificationService();

      expect(service1).toBe(service2);
    });
  });

  describe('CredentialNotificationTemplates', () => {
    it('should have ISSUED template', () => {
      expect(CredentialNotificationTemplates.ISSUED).toBeDefined();
      expect(CredentialNotificationTemplates.ISSUED.email).toBeDefined();
      expect(CredentialNotificationTemplates.ISSUED.sms).toBeDefined();
      expect(CredentialNotificationTemplates.ISSUED.push).toBeDefined();
    });

    it('should have RENEWED template', () => {
      expect(CredentialNotificationTemplates.RENEWED).toBeDefined();
    });

    it('should have EXPIRING template', () => {
      expect(CredentialNotificationTemplates.EXPIRING).toBeDefined();
    });

    it('should have REVOKED template', () => {
      expect(CredentialNotificationTemplates.REVOKED).toBeDefined();
    });
  });
});

