import * as fs from 'fs';
import * as path from 'path';
import * as Handlebars from 'handlebars';

describe('mail templates rendering', () => {
  const templatesDir = __dirname;

  beforeAll(() => {
    const layoutSource = fs.readFileSync(
      path.join(templatesDir, 'partials', 'layout.hbs'),
      'utf-8',
    );
    Handlebars.registerPartial('layout', layoutSource);
  });

  const baseContext = {
    title: 'Test-Betreff',
    year: 2026,
    firstName: 'Max',
    url: 'https://app.example.com/confirm-email/abc123',
    actionTitle: 'Jetzt bestätigen',
  };

  it('renders activation.hbs with greeting, link and button text', () => {
    const source = fs.readFileSync(
      path.join(templatesDir, 'activation.hbs'),
      'utf-8',
    );
    const html = Handlebars.compile(source, { strict: true })(baseContext);

    expect(html).toContain('TeamWallet');
    expect(html).toContain('Hallo Max,');
    expect(html).toContain(baseContext.url);
    expect(html).toContain(baseContext.actionTitle);
  });

  it('renders reset-password.hbs with greeting, link and button text', () => {
    const source = fs.readFileSync(
      path.join(templatesDir, 'reset-password.hbs'),
      'utf-8',
    );
    const html = Handlebars.compile(source, { strict: true })(baseContext);

    expect(html).toContain('TeamWallet');
    expect(html).toContain('Hallo Max,');
    expect(html).toContain(baseContext.url);
    expect(html).toContain(baseContext.actionTitle);
  });

  it('falls back to a generic greeting when firstName is missing', () => {
    const source = fs.readFileSync(
      path.join(templatesDir, 'activation.hbs'),
      'utf-8',
    );
    const html = Handlebars.compile(source, { strict: true })({
      ...baseContext,
      firstName: undefined,
    });

    expect(html).toContain('Hallo,');
    expect(html).not.toContain('Hallo Max,');
  });
});
