const moment = require('moment');

const { filterIncompleteSections, hasOutcomes, isSectionComplete, getEventDate } = require('./assessment.helper');
const EDGE_EVENT = require('../constant/relationship/edge-event.constant');
const EDGE_ROLE = require('../constant/relationship/edge-role.constant');

const TEMPLATE = {
  label: 'Laboratory Hazard Assessment',
  questions: [
    {
      type: 'group',
      key: 'chemical',
      label: 'Chemical Hazards',
      required: true,
      meta: {
        description: 'Do you have any chemical hazards in your location?',
      },
      questions: [
        {
          type: 'radio',
          key: 'C2',
          label: 'Working with hazardous liquids or other materials which create a splash hazard',
          required: true,
          options: [
            {
              label: 'Yes',
              value: 'Yes',
            },
            {
              label: 'No',
              value: 'No',
            },
          ],
        },
      ],
    },
    {
      type: 'group',
      key: 'physical',
      label: 'Physical Hazards',
      required: true,
      questions: [
        {
          type: 'radio',
          key: 'P1',
          label: 'Working with cryogenic liquids',
          required: true,
          options: [
            {
              label: 'Yes',
              value: 'Yes',
            },
            {
              label: 'No',
              value: 'No',
            },
          ],
        },
        {
          type: 'radio',
          key: 'P2',
          label: 'Working with very cold equipment, samples, or dry ice',
          required: true,
          options: [
            {
              label: 'Yes',
              value: 'Yes',
            },
            {
              label: 'No',
              value: 'No',
            },
          ],
        },
      ],
    },
    {
      type: 'group',
      key: 'custom',
      label: 'Custom Hazards',
      required: true,
      introDefaultQuestion: null,
      questions: [
        {
          type: 'addanother',
          key: 'CH1',
          label: 'Additional Hazardous Activities',
          required: false,
          questions: [
            {
              type: 'input',
              key: 'CH1a',
              label: 'Lab Activity',
            },
            {
              type: 'input',
              key: 'CH1b',
              label: 'Potential Hazards',
            },
            {
              type: 'input',
              key: 'CH1c',
              label: "Active Researchers' PPE",
            },
            {
              type: 'input',
              key: 'CH1d',
              label: "Adjacent Individuals' PPE",
            },
          ],
          defaultNo: null,
          defaultYes: null,
          info: 'Any hazards not covered in Chemical, Physical, Biological, Radiological, Laser, Non-Ionizing Radiation Hazards.',
        },
      ],
    },
  ],
};

const ASSESSMENT = {
  responses: {
    chemical: {
      C2: 'No',
    },
    physical: {
      P1: 'Yes',
    },
  },
};

const ASSESSMENT_REPORT_SUMMARY = {
  report: {
    summary: {
      key: 'summary1',
      conditions: [
        {
          path: 'RL.RLGQ1.RLQ1',
          value: '3 – Significate Risk',
          recommendation: 'RL.RLGQ1.RLQ1D.RLQ1_rec',
        },
      ],
    },
  },
};

const ASSESSMENT_REPORT_OUTCOME = {
  report: {
    outcomes: {
      'Active Researchers PPE': [
        {
          key: 'ppe6',
          category: "Active Researchers' PPE",
          name: 'Lab coat',
          conditions: [
            {
              path: 'physical.P5',
              value: 'Yes',
            },
          ],
        },
      ],
    },
  },
};

describe('Assessment Helper', () => {
  it('should return true for isSectionComplete for section of unrequired questions', () => {
    expect(isSectionComplete(ASSESSMENT, TEMPLATE.questions[2])).toEqual(true);
  });

  it('should return a list of incomplete sections for an assessment', () => {
    const INCOMPLETE_SECTIONS = [TEMPLATE.questions[1]];
    expect(filterIncompleteSections(TEMPLATE, ASSESSMENT)).toEqual(INCOMPLETE_SECTIONS);
  });

  it('should return true for hasOutcomes, if report has summary or outcomes', () => {
    expect(hasOutcomes(ASSESSMENT_REPORT_SUMMARY)).toEqual(true);
    expect(hasOutcomes(ASSESSMENT_REPORT_OUTCOME)).toEqual(true);
  });

  it('should return the latest certified date', () => {
    const latestDate = new Date('10/10/20');
    const earlierDate = new Date('10/08/20');
    const momentDate = moment(latestDate);
    const assessment = {
      relationships: [
        {
          edge: {
            id: '32bd3190-6568-3e32-b3fd-6ff8e5e8a760',
            label: 'ACKNOWLEDGED',
            type: 'EVENT',
            isActive: true,
            startDate: '2021-07-01T17:32:11.313Z',
            primary: false,
          },
          node: {
            id: 'ab8f5e9dcb8cc2a2022e9c46ce311380',
            label: 'USER',
            lastName: 'Schmidt',
            firstName: 'Matthew',
            email: 'mnschmidt@ucdavis.edu',
          },
        },
        {
          edge: {
            id: 'a6bbc547-b98d-7d68-e1fa-600840f84eb3',
            label: 'CERTIFIED',
            type: 'EVENT',
            isActive: true,
            startDate: earlierDate,
            primary: false,
            migrated: true,
          },
          node: {
            id: 'VUN8MDN8aHlzbG9wQHVjZGF2aXMuZWR1',
            label: 'USER',
            lastName: 'Hyslop',
            firstName: 'Nicole',
            email: 'nmhyslop@ucdavis.edu',
          },
        },
        {
          edge: {
            id: 'e4bf7323-911c-5dad-f9d7-5b8096204a31',
            label: 'DOCUMENT',
            type: 'ROLE',
            isActive: true,
            startDate: '2022-02-11T00:55:28.557Z',
            primary: false,
          },
          node: {
            id: '619cf0635d35305d3571349b',
            label: 'LAB_HAZARD_CONFIG',
            name: 'Lab Hazard Assessments',
            createdDate: '2022-02-11T00:50:56.867Z',
            type: 'LAB_HAZARD',
            url: 'lab-hazard',
          },
        },
        {
          edge: {
            id: 'd8be0d12-8173-3e49-1ffd-009f2ebd88f4',
            label: 'CERTIFIED',
            type: 'EVENT',
            isActive: true,
            startDate: latestDate,
            primary: false,
          },
          node: {
            id: 'VUN8MDN8ZGlsbG5lckB1Y2RhdmlzLmVkdQ',
            label: 'USER',
            lastName: 'Dillner',
            firstName: 'Ann',
            email: 'amdillner@ucdavis.edu',
          },
        },
      ],
    };
    expect(getEventDate(assessment, EDGE_EVENT.CERTIFIED)).toEqual(momentDate);
  });

  it('should return empty string if there is no certified date', () => {
    const assessment = {
      relationships: [
        {
          startDate: new Date(),
          role: EDGE_ROLE.OWNER,
          events: [],
        },
      ],
    };
    expect(getEventDate(assessment)).toEqual('');
  });
});
