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

medication_name: {
        type: DataTypes.TEXT,
      
      

      },

dosage: {
        type: DataTypes.TEXT,
      
      

      },

route: {
        type: DataTypes.TEXT,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  medications.associate = (db) => {


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









    db.medications.hasMany(db.medication_schedules, {
      as: 'medication_schedules_medication',
      foreignKey: {
          name: 'medicationId',
      },
      constraints: false,
    });


    db.medications.hasMany(db.calendar_events, {
      as: 'calendar_events_linked_medication',
      foreignKey: {
          name: 'linked_medicationId',
      },
      constraints: false,
    });




















//end loop



    db.medications.belongsTo(db.patients, {
      as: 'patient',
      foreignKey: {
        name: 'patientId',
      },
      constraints: false,
    });

    db.medications.belongsTo(db.users, {
      as: 'prescribed_by',
      foreignKey: {
        name: 'prescribed_byId',
      },
      constraints: false,
    });

    db.medications.belongsTo(db.accounts, {
      as: 'accounts',
      foreignKey: {
        name: 'accountsId',
      },
      constraints: false,
    });




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

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



  return medications;
};


