import assert from 'node:assert/strict';
import test from 'node:test';
import {
  resolveDocumentSequenceProductTypeInput,
  type DocumentSequenceProductTypeLookupOption
} from './product-type-resolver';

const productTypeOptions: DocumentSequenceProductTypeLookupOption[] = [
  { id: 'product-crane', code: 'crane', label: 'Crane' },
  { id: 'product-dockdoor', code: 'dockdoor', label: 'Dock Door' },
  { id: 'product-solarcell', code: 'solarcell', label: 'Solar Cell' },
  { id: 'product-service', code: 'service', label: 'Service' },
  { id: 'product-sparepart', code: 'sparepart', label: 'Spare Part' }
];

const quotationTypeOptions: DocumentSequenceProductTypeLookupOption[] = [
  { id: 'quotation-crane', code: 'crane', label: 'Crane' },
  { id: 'quotation-dockdoor', code: 'dockdoor', label: 'Dock Door' },
  { id: 'quotation-solarcell', code: 'solarcell', label: 'Solar Cell' },
  { id: 'quotation-service', code: 'service', label: 'Service' }
];

test('resolveDocumentSequenceProductTypeInput resolves quotation type option ids', () => {
  const resolution = resolveDocumentSequenceProductTypeInput({
    documentType: 'quotation',
    productType: 'quotation-crane',
    options: productTypeOptions,
    referenceOptions: quotationTypeOptions
  });

  assert.deepEqual(resolution, {
    productType: 'crane',
    matchedBy: 'option-id',
    normalizedRequestedProductType: 'quotation_crane'
  });
});

test('resolveDocumentSequenceProductTypeInput resolves quotation type codes directly', () => {
  const resolution = resolveDocumentSequenceProductTypeInput({
    documentType: 'quotation',
    productType: 'service',
    options: productTypeOptions,
    referenceOptions: quotationTypeOptions
  });

  assert.deepEqual(resolution, {
    productType: 'service',
    matchedBy: 'option-code',
    normalizedRequestedProductType: 'service'
  });
});

test('resolveDocumentSequenceProductTypeInput resolves document-sequence aliases', () => {
  const dockDoorResolution = resolveDocumentSequenceProductTypeInput({
    documentType: 'quotation',
    productType: 'dock_door',
    options: productTypeOptions,
    referenceOptions: quotationTypeOptions
  });
  const solarResolution = resolveDocumentSequenceProductTypeInput({
    documentType: 'quotation',
    productType: 'solar',
    options: productTypeOptions,
    referenceOptions: quotationTypeOptions
  });

  assert.deepEqual(dockDoorResolution, {
    productType: 'dockdoor',
    matchedBy: 'sequence-alias',
    normalizedRequestedProductType: 'dock_door'
  });
  assert.deepEqual(solarResolution, {
    productType: 'solarcell',
    matchedBy: 'sequence-alias',
    normalizedRequestedProductType: 'solar'
  });
});

test('resolveDocumentSequenceProductTypeInput returns generic sequence fallback for non-quotation documents', () => {
  const resolution = resolveDocumentSequenceProductTypeInput({
    documentType: 'customer',
    productType: undefined,
    options: productTypeOptions
  });

  assert.deepEqual(resolution, {
    productType: 'all',
    matchedBy: 'generic',
    normalizedRequestedProductType: null
  });
});

test('resolveDocumentSequenceProductTypeInput reports unresolved quotation inputs', () => {
  const resolution = resolveDocumentSequenceProductTypeInput({
    documentType: 'quotation',
    productType: 'other',
    options: productTypeOptions,
    referenceOptions: quotationTypeOptions
  });

  assert.deepEqual(resolution, {
    productType: null,
    matchedBy: 'generic',
    normalizedRequestedProductType: 'other'
  });
});
