import { describe, expect, it } from 'vitest';

import { createTemplateSchema } from '@/lib/validators/document-templates';

const baseInput = {
  name: 'Tmpl',
  templateType: 'custom' as const,
  bodyHtml: '<p>x</p>',
};

describe('createTemplateSchema - mergeFields allow-list', () => {
  it('accepts valid tokens from the catalog', () => {
    const parsed = createTemplateSchema.parse({
      ...baseInput,
      mergeFields: ['{{client.fullName}}', '{{yacht.name}}', '{{berth.mooringNumber}}'],
    });
    expect(parsed.mergeFields).toEqual([
      '{{client.fullName}}',
      '{{yacht.name}}',
      '{{berth.mooringNumber}}',
    ]);
  });

  it('rejects deprecated tokens that lived on `clients` before the refactor', () => {
    const result = createTemplateSchema.safeParse({
      ...baseInput,
      mergeFields: ['{{client.yachtName}}'],
    });
    expect(result.success).toBe(false);
    if (!result.success) {
      expect(result.error.issues[0]?.message).toMatch(/Unknown merge tokens.*client\.yachtName/);
    }
  });

  it('rejects unknown tokens with a helpful message listing them', () => {
    const result = createTemplateSchema.safeParse({
      ...baseInput,
      mergeFields: ['{{client.fullName}}', '{{not.a.token}}', '{{also.bogus}}'],
    });
    expect(result.success).toBe(false);
    if (!result.success) {
      expect(result.error.issues[0]?.message).toContain('not.a.token');
      expect(result.error.issues[0]?.message).toContain('also.bogus');
    }
  });

  it('defaults mergeFields to an empty array when omitted', () => {
    const parsed = createTemplateSchema.parse(baseInput);
    expect(parsed.mergeFields).toEqual([]);
  });

  it('accepts an empty mergeFields array', () => {
    const parsed = createTemplateSchema.parse({ ...baseInput, mergeFields: [] });
    expect(parsed.mergeFields).toEqual([]);
  });

  it('allows the new `eoi` templateType', () => {
    const parsed = createTemplateSchema.parse({ ...baseInput, templateType: 'eoi' });
    expect(parsed.templateType).toBe('eoi');
  });
});
