import { ConfigService } from '@nestjs/config';

import { EmailService } from './email.service';

describe('EmailService', () => {
  const email = {
    to: [{ email: 'person@example.com', name: 'Portal User' }],
    subject: 'Test message',
    textContent: 'This is a test message.',
  };

  it('does not initialize Brevo or send while email is disabled', async () => {
    const configService = {
      get: jest.fn().mockReturnValue('false'),
      getOrThrow: jest.fn(),
    } as unknown as ConfigService;
    const service = new EmailService(configService);

    await expect(service.sendTransactionalEmail(email)).resolves.toEqual({
      status: 'skipped',
      reason: 'email_disabled',
    });
    expect(configService.getOrThrow).not.toHaveBeenCalled();
  });

  it('skips a welcome email while email is disabled', async () => {
    const configService = {
      get: jest.fn().mockReturnValue('false'),
      getOrThrow: jest.fn(),
    } as unknown as ConfigService;
    const service = new EmailService(configService);

    await expect(
      service.sendWelcomeEmail({
        email: 'person@example.com',
        name: 'Portal User',
      }),
    ).resolves.toEqual({
      status: 'skipped',
      reason: 'email_disabled',
    });
    expect(configService.getOrThrow).not.toHaveBeenCalled();
  });

  it('validates an inline email before a provider request', async () => {
    const configService = {
      get: jest.fn().mockReturnValue('true'),
      getOrThrow: jest.fn(),
    } as unknown as ConfigService;
    const service = new EmailService(configService);

    await expect(
      service.sendTransactionalEmail({
        to: [{ email: 'person@example.com' }],
      }),
    ).rejects.toThrow('A subject is required when no Brevo template is used.');
    expect(configService.getOrThrow).not.toHaveBeenCalled();
  });
});
