import CfExceptionHandler from "../../../core/CfExceptionHandler";
import { setToUndefinedIfNull } from "../../../utils";
import {
    AppointmentProperties, DiagnosisProperties,
    EncounterProperties,
    PatientMgmtEventType,
} from "../typings";
import { PatientProperties } from "../typings";

export const validatePatientEvent = (properties: PatientProperties) => {
  if (properties.patient_id == undefined || properties.patient_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Patient,
      "patient id required"
    );
    return null;
  } else if (properties.site_id == undefined || properties.site_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Patient,
      "site_id is required"
    );
    return null;
  } else if (properties.action == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Patient,
      "action is required"
    );
    return null;
  } else if (properties.type == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Patient,
      "type is required"
    );
    return null;
  }

  properties = setToUndefinedIfNull(properties, [
    "sub_type",
    "biometric_list",
    "pregnancy_details",
    "registration_date",
  ]);

  if (properties.is_from_gho == undefined) {
    properties.is_from_gho = false;
  }

  return properties;
};

export const validateEncounterEvent = (properties: EncounterProperties) => {
  if (properties.patient_id == undefined || properties.patient_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Encounter,
      "encounter id required"
    );
    return null;
  } else if (properties.site_id == undefined || properties.site_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Encounter,
      "site_id is required"
    );
    return null;
  } else if (properties.action == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Encounter,
      "action is required"
    );
    return null;
  } else if (properties.category == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Encounter,
      "category is required"
    );
    return null;
  } else if (properties.type == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Encounter,
      "type is required"
    );
    return null;
  } else if (properties.sub_type == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Encounter,
      "sub_type is required"
    );
    return null;
  }

  return properties;
};

export const validateAppointmentEvent = (properties: AppointmentProperties) => {
  if (properties.patient_id == undefined || properties.patient_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "Patient ID is required"
    );
    return null;
  } else if (properties.site_id == undefined || properties.site_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "site ID is required"
    );
    return null;
  } else if (properties.appointment_id == undefined || properties.appointment_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "Appointment ID is required"
    );
    return null;
  } else if (properties.action == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "action is required"
    );
    return null;
  }

  return properties;
};

export const validateDiagnosisEvent = (properties: DiagnosisProperties) => {
  if (properties.patient_id == undefined || properties.patient_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "Patient ID is required"
    );
    return null;
  } else if (properties.site_id == undefined || properties.site_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "site ID is required"
    );
    return null;
  } else if (properties.encounter_id == undefined || properties.encounter_id == "") {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "Appointment ID is required"
    );
    return null;
  } else if (properties.action == undefined) {
    new CfExceptionHandler().throwAndLogError(
      PatientMgmtEventType.Appointment,
      "action is required"
    );
    return null;
  }

  return properties;
};
