import { Test, TestingModule } from '@nestjs/testing';
import { MailService, ValidationAccountEmailKind } from './mail.service';
import { AppConfigService } from '../config/config.service';

describe('MailService (ticket #28)', () => {
  let service: MailService;
  let sendEmailSpy: jest.SpyInstance;

  const mockConfigGet = jest.fn((key: string, defaultValue?: unknown) => {
    const values: Record<string, unknown> = {
      app_name: 'TestApp',
      app_url: 'https://app.test/',
      smtp_host: '127.0.0.1',
      smtp_port: 1025,
      smtp_secure: false,
      smtp_auth_required: false,
      smtp_user: '',
      smtp_password: '',
      email_from_name: 'Test',
      email_from_address: 'noreply@test',
    };
    if (key in values) return values[key];
    return defaultValue;
  });

  beforeEach(async () => {
    jest.clearAllMocks();
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        MailService,
        {
          provide: AppConfigService,
          useValue: { get: mockConfigGet },
        },
      ],
    }).compile();

    service = module.get<MailService>(MailService);
    sendEmailSpy = jest.spyOn(service, 'sendEmail').mockResolvedValue(undefined);
  });

  afterEach(() => {
    sendEmailSpy.mockRestore();
  });

  it.each<[ValidationAccountEmailKind, string]>([
    ['parent', 'Compte parent validé'],
    ['am', 'Inscription validée'],
  ])('sendValidatedAccountPasswordSetupEmail (%s) utilise Handlebars + lien token', async (kind, subjectPart) => {
    const token = '11111111-2222-3333-4444-555555555555';
    await service.sendValidatedAccountPasswordSetupEmail(
      {
        email: 'user@test.fr',
        prenom: 'Jean',
        nom: 'Dupont',
        token,
        numeroDossier: '2025-000001',
      },
      kind,
    );

    expect(sendEmailSpy).toHaveBeenCalledTimes(1);
    const [, subject, html] = sendEmailSpy.mock.calls[0];
    expect(subject).toContain('TestApp');
    expect(subject).toContain(subjectPart);
    expect(html).toContain('Jean');
    expect(html).toContain('Dupont');
    expect(html).toContain('2025-000001');
    expect(html).toContain(`https://app.test/create-password?token=${encodeURIComponent(token)}`);
  });

  it('sendPasswordResetEmail utilise Handlebars + lien /reset-password', async () => {
    const token = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee';
    await service.sendPasswordResetEmail({
      email: 'u@test.fr',
      prenom: 'Marie',
      nom: 'Curie',
      token,
    });
    expect(sendEmailSpy).toHaveBeenCalledTimes(1);
    const [, subject, html] = sendEmailSpy.mock.calls[0];
    expect(subject).toContain('Réinitialisation');
    expect(html).toContain('Marie');
    expect(html).toContain(`https://app.test/reset-password?token=${encodeURIComponent(token)}`);
    expect(html).not.toContain('create-password');
  });
});
