import * as path from 'path';
import { ConfigService } from '@nestjs/config';
import { MailConfigService } from './mail-config.service';

describe('MailConfigService integration', () => {
  it('produces mailer options whose adapter actually renders the shared layout partial', (done) => {
    const workingDirectory = path.join(__dirname, '..', '..');
    const configValues: Record<string, unknown> = {
      'app.workingDirectory': workingDirectory,
      'mail.host': 'localhost',
      'mail.port': 1025,
      'mail.ignoreTLS': true,
      'mail.secure': false,
      'mail.requireTLS': false,
      'mail.user': '',
      'mail.password': '',
      'mail.defaultName': 'TeamWallet',
      'mail.defaultEmail': 'test@example.com',
    };
    const configService = {
      get: (key: string) => configValues[key],
    } as unknown as ConfigService;

    const options = new MailConfigService(configService).createMailerOptions();

    const mail: {
      data: {
        template: string;
        context: Record<string, unknown>;
        html?: string;
      };
    } = {
      data: {
        template: 'activation',
        context: {
          title: 'Test',
          year: 2026,
          firstName: 'Max',
          url: 'https://example.com/confirm-email/abc',
          actionTitle: 'Jetzt bestätigen',
        },
      },
    };

    options.template.adapter.compile(
      mail,
      (err?: Error) => {
        expect(err).toBeUndefined();
        expect(mail.data.html).toContain('TeamWallet');
        expect(mail.data.html).toContain('tw-wordmark');
        done();
      },
      options,
    );
  });
});
