import {
  clearanceSettingCode,
  clearanceOutputSettingCode,
  clearanceCodesForBooking,
  INTERCITY_DOCUMENTS_SETTING_CODE,
} from './clearance.util';
import type { Booking } from './entities/booking.entity';

describe('clearance.util — clearanceSettingCode', () => {
  it('resolves import container with/without customs', () => {
    expect(clearanceSettingCode('IMPORT', 'CONTAINER', true)).toBe(
      'clearance_import_container_with_customs',
    );
    // Non-customs bookings self-clear with the same document set a ONE_TIME
    // self-clear contract uses.
    expect(clearanceSettingCode('IMPORT', 'CONTAINER', false)).toBe(
      'contract_clearance_selfclear_import_container',
    );
  });

  it('resolves export bulk with/without customs', () => {
    expect(clearanceSettingCode('EXPORT', 'BULK', true)).toBe(
      'clearance_export_bulk_with_customs',
    );
    expect(clearanceSettingCode('EXPORT', 'BULK', false)).toBe(
      'contract_clearance_selfclear_export_bulk',
    );
  });

  it('resolves the intercity document set for DOMESTIC regardless of customs/freight', () => {
    expect(clearanceSettingCode('DOMESTIC', 'CONTAINER', true)).toBe(
      INTERCITY_DOCUMENTS_SETTING_CODE,
    );
    expect(clearanceSettingCode('DOMESTIC', 'BULK', false)).toBe(
      INTERCITY_DOCUMENTS_SETTING_CODE,
    );
  });
});

describe('clearance.util — clearanceCodesForBooking (intercity)', () => {
  const base = {
    tradeDirection: 'DOMESTIC',
    freightType: 'CONTAINER',
    serviceType: null,
    customsClearingEnabled: false,
  };

  it('GENERAL drawdowns and direct bookings carry the per-booking intercity set', () => {
    const general = clearanceCodesForBooking({
      ...base,
      contractId: 'c1',
      contractKind: 'GENERAL',
    } as unknown as Booking);
    expect(general.inputCode).toBe(INTERCITY_DOCUMENTS_SETTING_CODE);
    expect(general.outputCode).toBeNull();

    const direct = clearanceCodesForBooking({
      ...base,
      contractId: null,
      contractKind: null,
    } as unknown as Booking);
    expect(direct.inputCode).toBe(INTERCITY_DOCUMENTS_SETTING_CODE);
  });

  it('ONE_TIME contract drawdowns skip the per-booking set (contract collected it)', () => {
    const drawdown = clearanceCodesForBooking({
      ...base,
      contractId: 'c1',
      contractKind: 'ONE_TIME',
    } as unknown as Booking);
    expect(drawdown.inputCode).toBeNull();
    expect(drawdown.outputCode).toBeNull();
  });
});

describe('clearance.util — clearanceOutputSettingCode', () => {
  it('returns a container output code only for customs container bookings', () => {
    expect(clearanceOutputSettingCode('IMPORT', 'CONTAINER', true)).toBe(
      'clearance_output_import_container',
    );
    expect(clearanceOutputSettingCode('EXPORT', 'CONTAINER', true)).toBe(
      'clearance_output_export_container',
    );
  });

  it('returns null without customs', () => {
    expect(clearanceOutputSettingCode('IMPORT', 'CONTAINER', false)).toBeNull();
  });

  it('resolves bulk output sets (mirrors container) and returns null for domestic', () => {
    expect(clearanceOutputSettingCode('IMPORT', 'BULK', true)).toBe(
      'clearance_output_import_bulk',
    );
    expect(clearanceOutputSettingCode('EXPORT', 'BULK', true)).toBe(
      'clearance_output_export_bulk',
    );
    expect(clearanceOutputSettingCode('DOMESTIC', 'CONTAINER', true)).toBeNull();
  });
});
