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

full_name: {
        type: DataTypes.TEXT,
      
      

      },

dho_code: {
        type: DataTypes.TEXT,
      
      

      },

doctor_registration_no: {
        type: DataTypes.TEXT,
      
      

      },

assigned_region: {
        type: DataTypes.TEXT,
      
      

      },

subcenter_name: {
        type: DataTypes.TEXT,
      
      

      },

subcenter_latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

subcenter_longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

availability_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"online",


"offline",


"busy",


"in_field"

        ],

      },

can_register_patients: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  rural_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.rural_doctors.hasMany(db.patients, {
      as: 'patients_registered_by_rural_doctor',
      foreignKey: {
          name: 'registered_by_rural_doctorId',
      },
      constraints: false,
    });


    db.rural_doctors.hasMany(db.medical_reports, {
      as: 'medical_reports_author_rural_doctor',
      foreignKey: {
          name: 'author_rural_doctorId',
      },
      constraints: false,
    });



    db.rural_doctors.hasMany(db.inventory_updates, {
      as: 'inventory_updates_rural_doctor',
      foreignKey: {
          name: 'rural_doctorId',
      },
      constraints: false,
    });


    db.rural_doctors.hasMany(db.medicine_dispatches, {
      as: 'medicine_dispatches_requested_by_rural_doctor',
      foreignKey: {
          name: 'requested_by_rural_doctorId',
      },
      constraints: false,
    });



    db.rural_doctors.hasMany(db.medical_camps, {
      as: 'medical_camps_lead_rural_doctor',
      foreignKey: {
          name: 'lead_rural_doctorId',
      },
      constraints: false,
    });


    db.rural_doctors.hasMany(db.appointments, {
      as: 'appointments_rural_doctor',
      foreignKey: {
          name: 'rural_doctorId',
      },
      constraints: false,
    });













//end loop



    db.rural_doctors.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });




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

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



  return rural_doctors;
};


