/**
 * Document Templates Tests
 */

import { describe, it, expect } from 'vitest';
import {
  createDocumentTemplate,
  getDocumentTemplate,
  renderDocumentTemplate,
  extractTemplateVariables,
} from '@the-order/database';

describe('Document Templates', () => {
  it('should create a template', async () => {
    const template = await createDocumentTemplate({
      name: 'Test Contract',
      template_content: 'This is a contract between {{party1}} and {{party2}}.',
      category: 'contract',
    });

    expect(template).toBeDefined();
    expect(template.name).toBe('Test Contract');
  });

  it('should render template with variables', async () => {
    const template = await createDocumentTemplate({
      name: 'Test Template',
      template_content: 'Hello {{name}}, welcome to {{company}}.',
    });

    const rendered = renderDocumentTemplate(template, {
      name: 'John Doe',
      company: 'Acme Corp',
    });

    expect(rendered).toContain('John Doe');
    expect(rendered).toContain('Acme Corp');
    expect(rendered).not.toContain('{{name}}');
    expect(rendered).not.toContain('{{company}}');
  });

  it('should extract variables from template', () => {
    const template_content = 'Contract between {{party1}} and {{party2}} dated {{date}}';
    const variables = extractTemplateVariables(template_content);

    expect(variables).toContain('party1');
    expect(variables).toContain('party2');
    expect(variables).toContain('date');
  });
});

