import { TemplateService } from '../services/template.service';

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

  beforeEach(() => {
    service = new TemplateService();
  });

  it('render returns rendered subject and body for user.registered template', () => {
    const result = service.render('user.registered', { phone: '0901234567', role: 'BUYER' });

    expect(result.subject).toBe('Chào mừng bạn đến với GoodGo!');
    expect(result.body).toContain('0901234567');
    expect(result.body).toContain('BUYER');
  });

  it('render returns rendered subject and body for quota.exceeded template', () => {
    const result = service.render('quota.exceeded', { metric: 'listings', used: 10, limit: 10 });

    expect(result.subject).toBe('Bạn đã đạt giới hạn sử dụng');
    expect(result.body).toContain('listings');
    expect(result.body).toContain('10');
  });

  it('render throws error for unknown template key', () => {
    expect(() => service.render('does.not.exist', {})).toThrow(
      'Notification template "does.not.exist" not found',
    );
  });

  it('hasTemplate returns true for existing template', () => {
    expect(service.hasTemplate('user.registered')).toBe(true);
    expect(service.hasTemplate('agent.verified')).toBe(true);
    expect(service.hasTemplate('quota.exceeded')).toBe(true);
  });

  it('hasTemplate returns false for non-existing template', () => {
    expect(service.hasTemplate('unknown.template')).toBe(false);
  });

  it('getTemplateKeys returns all 9 template keys', () => {
    const keys = service.getTemplateKeys();

    expect(keys).toHaveLength(9);
    expect(keys).toContain('user.registered');
    expect(keys).toContain('agent.verified');
    expect(keys).toContain('listing.approved');
    expect(keys).toContain('listing.rejected');
    expect(keys).toContain('inquiry.received');
    expect(keys).toContain('quota.exceeded');
    expect(keys).toContain('password.reset');
    expect(keys).toContain('payment.confirmed');
    expect(keys).toContain('subscription.expiring');
  });
});
