import { AppException } from '../../common/errors';
import { assertCompleteToothProsthesisMap } from './lab-case-send.validation';

describe('assertCompleteToothProsthesisMap', () => {
  const prosthesisDetailId = 'detail-1';

  it('passes when every prosthesis tooth has a mapping', () => {
    expect(() =>
      assertCompleteToothProsthesisMap({
        details: [
          {
            treatmentDetailId: prosthesisDetailId,
            detail: { id: prosthesisDetailId, treatmentType: 'prosthesis', teeth: ['14', '15'] },
          },
        ],
        toothProsthesis: [
          { treatmentDetailId: prosthesisDetailId, tooth: '14', prosthesisTypeCode: 'pfm_crown' },
          { treatmentDetailId: prosthesisDetailId, tooth: '15', prosthesisTypeCode: 'pfm_crown' },
        ],
      }),
    ).not.toThrow();
  });

  it('ignores non-prosthesis details', () => {
    expect(() =>
      assertCompleteToothProsthesisMap({
        details: [
          {
            treatmentDetailId: 'endo-1',
            detail: { id: 'endo-1', treatmentType: 'endo', teeth: ['36'] },
          },
        ],
        toothProsthesis: [],
      }),
    ).not.toThrow();
  });

  it('passes when there are no FDI teeth but an arch sentinel is mapped', () => {
    expect(() =>
      assertCompleteToothProsthesisMap({
        details: [
          {
            treatmentDetailId: prosthesisDetailId,
            detail: { id: prosthesisDetailId, treatmentType: 'prosthesis', teeth: [] },
          },
        ],
        toothProsthesis: [
          { treatmentDetailId: prosthesisDetailId, tooth: 'UA', prosthesisTypeCode: 'full_denture' },
        ],
      }),
    ).not.toThrow();
  });

  it('throws when a prosthesis detail has neither teeth nor an arch job', () => {
    expect(() =>
      assertCompleteToothProsthesisMap({
        details: [
          {
            treatmentDetailId: prosthesisDetailId,
            detail: { id: prosthesisDetailId, treatmentType: 'prosthesis', teeth: [] },
          },
        ],
        toothProsthesis: [],
      }),
    ).toThrow(AppException);
  });

  it('throws when a prosthesis tooth is missing from the map', () => {
    expect(() =>
      assertCompleteToothProsthesisMap({
        details: [
          {
            treatmentDetailId: prosthesisDetailId,
            detail: { id: prosthesisDetailId, treatmentType: 'prosthesis', teeth: ['14', '15'] },
          },
        ],
        toothProsthesis: [
          { treatmentDetailId: prosthesisDetailId, tooth: '14', prosthesisTypeCode: 'pfm_crown' },
        ],
      }),
    ).toThrow(AppException);
  });
});
