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 doctors = sequelize.define(
    'doctors',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

full_name: {
        type: DataTypes.TEXT,
      
      

      },

national_number: {
        type: DataTypes.TEXT,
      
      

      },

hire_date: {
        type: DataTypes.DATE,
      
      

      },

specialty: {
        type: DataTypes.TEXT,
      
      

      },

visit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

doctor_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"external",


"internal",


"resident"

        ],

      },

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

  doctors.associate = (db) => {


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





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




    db.doctors.hasMany(db.visits, {
      as: 'visits_doctor',
      foreignKey: {
          name: 'doctorId',
      },
      constraints: false,
    });


    db.doctors.hasMany(db.orders, {
      as: 'orders_requested_by',
      foreignKey: {
          name: 'requested_byId',
      },
      constraints: false,
    });









    db.doctors.hasMany(db.endoscopy_cases, {
      as: 'endoscopy_cases_doctor',
      foreignKey: {
          name: 'doctorId',
      },
      constraints: false,
    });


    db.doctors.hasMany(db.surgery_cases, {
      as: 'surgery_cases_surgeon',
      foreignKey: {
          name: 'surgeonId',
      },
      constraints: false,
    });




    db.doctors.hasMany(db.cathlab_cases, {
      as: 'cathlab_cases_doctor',
      foreignKey: {
          name: 'doctorId',
      },
      constraints: false,
    });


    db.doctors.hasMany(db.ecg_stress_tests, {
      as: 'ecg_stress_tests_doctor',
      foreignKey: {
          name: 'doctorId',
      },
      constraints: false,
    });















    db.doctors.hasMany(db.room_bookings, {
      as: 'room_bookings_supervising_doctor',
      foreignKey: {
          name: 'supervising_doctorId',
      },
      constraints: false,
    });




    db.doctors.hasMany(db.maternity_cases, {
      as: 'maternity_cases_doctor',
      foreignKey: {
          name: 'doctorId',
      },
      constraints: false,
    });



    db.doctors.hasMany(db.nicu_incubators, {
      as: 'nicu_incubators_admitting_doctor',
      foreignKey: {
          name: 'admitting_doctorId',
      },
      constraints: false,
    });






//end loop



    db.doctors.belongsTo(db.users, {
      as: 'user_account',
      foreignKey: {
        name: 'user_accountId',
      },
      constraints: false,
    });




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

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



  return doctors;
};


