import {
  CONTRACT_TEMPLATE_KEYS,
  CONTRACT_TEMPLATE_REGISTRY,
  getTemplateMeta,
  isValidTemplateKey,
} from './contract-template.registry';

describe('ContractTemplateRegistry', () => {
  it('defines exactly 24 template keys', () => {
    expect(CONTRACT_TEMPLATE_KEYS).toHaveLength(24);
    expect(Object.keys(CONTRACT_TEMPLATE_REGISTRY)).toHaveLength(24);
  });

  it('keys match the direction_freight_currency_service pattern', () => {
    for (const key of CONTRACT_TEMPLATE_KEYS) {
      expect(isValidTemplateKey(key)).toBe(true);
    }
  });

  it('each meta has non-empty obligations and article1 scope', () => {
    for (const key of CONTRACT_TEMPLATE_KEYS) {
      const meta = CONTRACT_TEMPLATE_REGISTRY[key]!;
      expect(meta.clientObligations.length).toBeGreaterThan(0);
      expect(meta.providerObligations.length).toBeGreaterThan(0);
      expect(meta.article1.scope.length).toBeGreaterThan(0);
      expect(meta.article1.objective.length).toBeGreaterThan(0);
      expect(meta.contractDocuments.length).toBeGreaterThan(0);
    }
  });

  it('IMP_CON_ETB_TRANSPORT_ONLY retains import container flagship clauses', () => {
    const meta = getTemplateMeta('IMP_CON_ETB_TRANSPORT_ONLY');
    expect(meta.direction).toBe('IMP');
    expect(meta.freight).toBe('CON');
    expect(meta.article1.objective).toContain('SGTD');
    expect(meta.article1.objective).toContain('empty container return');
    const clientText = meta.clientObligations.join(' ');
    expect(clientText).toContain('Djibouti Nagad');
    const providerText = meta.providerObligations.join(' ');
    expect(providerText).toContain('seven (7) calendar days');
  });

  it('EXP_CON_USD_TRANSPORT_ONLY uses export-oriented article1', () => {
    const meta = getTemplateMeta('EXP_CON_USD_TRANSPORT_ONLY');
    expect(meta.direction).toBe('EXP');
    expect(meta.article1.objective).toContain('SGTD');
    expect(meta.providerObligations.join(' ')).not.toContain(
      'Return empty containers from Dire Dawa',
    );
  });

  it('FORWARDING adds scope and provider obligations', () => {
    const transport = getTemplateMeta('IMP_CON_ETB_TRANSPORT_ONLY');
    const forwarding = getTemplateMeta('IMP_CON_ETB_FORWARDING');
    expect(forwarding.article1.scope.length).toBeGreaterThan(
      transport.article1.scope.length,
    );
    expect(forwarding.providerObligations.length).toBeGreaterThan(
      transport.providerObligations.length,
    );
  });

  it('getTemplateMeta fallback includes clause arrays for unknown keys', () => {
    const meta = getTemplateMeta('UNKNOWN_KEY');
    expect(meta.clientObligations.length).toBeGreaterThan(0);
    expect(meta.article1.scope.length).toBeGreaterThan(0);
  });
});
