const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const patients = sequelize.define(
    'patients',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

full_name: {
        type: DataTypes.TEXT,
      
      

      },

national_number: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

blood_group: {
        type: DataTypes.ENUM,
      
      

        values: [

"A+",


"A-",


"B+",


"B-",


"AB+",


"AB-",


"O+",


"O-"

        ],

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"male",


"female",


"other"

        ],

      },

birth_date: {
        type: DataTypes.DATE,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  patients.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity






    db.patients.hasMany(db.guarantors, {
      as: 'guarantors_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });



    db.patients.hasMany(db.visits, {
      as: 'visits_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.orders, {
      as: 'orders_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });










    db.patients.hasMany(db.surgery_cases, {
      as: 'surgery_cases_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });




    db.patients.hasMany(db.cathlab_cases, {
      as: 'cathlab_cases_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.ecg_stress_tests, {
      as: 'ecg_stress_tests_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });






    db.patients.hasMany(db.pharmacy_sales_invoices, {
      as: 'pharmacy_sales_invoices_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });




    db.patients.hasMany(db.patient_invoices, {
      as: 'patient_invoices_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });







    db.patients.hasMany(db.room_bookings, {
      as: 'room_bookings_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });




    db.patients.hasMany(db.maternity_cases, {
      as: 'maternity_cases_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });


    db.patients.hasMany(db.pregnancy_followups, {
      as: 'pregnancy_followups_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });



    db.patients.hasMany(db.daily_followup_clinic, {
      as: 'daily_followup_clinic_patient',
      foreignKey: {
          name: 'patientId',
      },
      constraints: false,
    });





//end loop



    db.patients.belongsTo(db.doctors, {
      as: 'primary_doctor',
      foreignKey: {
        name: 'primary_doctorId',
      },
      constraints: false,
    });




    db.patients.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.patients.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return patients;
};


