import { ResponseUuring } from '@/packages/shared/src/types/medipost-analysis';

import { AnalysisResponseElement } from '~/lib/types/analysis-response-element';

import {
  canCreateAnalysisResponseElement,
  getAnalysisResponseElementsForGroup,
} from './medipostPrivateMessage.service';

type TestExistingElement = Pick<
  AnalysisResponseElement,
  'analysis_element_original_id' | 'status' | 'response_value'
>;

describe('medipostPrivateMessage.service', () => {
  describe('canCreateAnalysisResponseElement', () => {
    it('should return true if the analysis response element does not exist', async () => {
      const existingElements = [] as TestExistingElement[];
      const groupUuring = {
        UuringuElement: {
          UuringOlek: 1,
          UuringId: '1',
        },
      } as const;
      const responseValue = 1;
      const log = jest.fn();
      expect(
        await canCreateAnalysisResponseElement({
          existingElements,
          groupUuring,
          responseValue,
          log,
        }),
      ).toBe(true);
    });

    it('should return false if the analysis response element exists and the status is higher', async () => {
      const existingElements = [
        { analysis_element_original_id: '1', status: '2', response_value: 1 },
      ] as TestExistingElement[];
      const groupUuring = {
        UuringuElement: {
          UuringOlek: 1,
          UuringId: '1',
        },
      } as const;
      const responseValue = 1;
      const log = jest.fn();
      expect(
        await canCreateAnalysisResponseElement({
          existingElements,
          groupUuring,
          responseValue,
          log,
        }),
      ).toBe(false);
    });
  });

  describe('getAnalysisResponseElementsForGroup', () => {
    it('should return single new element', async () => {
      const analysisGroup = {
        UuringuGruppNimi: '1',
        Uuring: [
          {
            UuringuElement: {
              UuringOlek: 1,
              UuringId: '1',
              UuringuVastus: [{ VastuseVaartus: '1' }],
            },
          },
        ] as unknown as ResponseUuring[],
      } as const;
      const existingElements = [] as TestExistingElement[];
      const log = jest.fn();
      expect(
        await getAnalysisResponseElementsForGroup({
          analysisGroup,
          existingElements,
          log,
        }),
      ).toEqual([
        {
          analysis_element_original_id: '1',
          analysis_name: undefined,
          comment: null,
          norm_lower: null,
          norm_lower_included: false,
          norm_status: undefined,
          norm_upper: null,
          norm_upper_included: false,
          response_time: null,
          response_value: 1,
          unit: null,
          original_response_element: {
            UuringOlek: 1,
            UuringId: '1',
            UuringuVastus: [{ VastuseVaartus: '1' }],
          },
          status: '1',
        },
      ]);
    });

    it('should return no new element if element already exists in higher status', async () => {
      const analysisGroup = {
        UuringuGruppNimi: '1',
        Uuring: [
          {
            UuringuElement: {
              UuringOlek: 1,
              UuringId: '1',
              UuringuVastus: [{ VastuseVaartus: '1' }],
            },
          },
        ] as unknown as ResponseUuring[],
      } as const;
      const existingElements = [
        { analysis_element_original_id: '1', status: '2', response_value: 1 },
      ] as TestExistingElement[];
      const log = jest.fn();
      expect(
        await getAnalysisResponseElementsForGroup({
          analysisGroup,
          existingElements,
          log,
        }),
      ).toEqual([]);
    });

    it('should return no new element if element already exists with response value', async () => {
      const analysisGroup = {
        UuringuGruppNimi: '1',
        Uuring: [
          {
            UuringuElement: {
              UuringOlek: 1,
              UuringId: '1',
              UuringuVastus: [{ VastuseVaartus: '' }],
            },
          },
        ] as unknown as ResponseUuring[],
      } as const;
      const existingElements = [
        { analysis_element_original_id: '1', status: '1', response_value: 1 },
      ] as TestExistingElement[];
      const log = jest.fn();
      expect(
        await getAnalysisResponseElementsForGroup({
          analysisGroup,
          existingElements,
          log,
        }),
      ).toEqual([]);
    });
  });
});
