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

dose_instructions: {
        type: DataTypes.TEXT,
      
      

      },

frequency: {
        type: DataTypes.TEXT,
      
      

      },

duration_days: {
        type: DataTypes.INTEGER,
      
      

      },

quantity_prescribed: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  prescription_items.associate = (db) => {


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



















    db.prescription_items.hasMany(db.reminders, {
      as: 'reminders_prescription_item',
      foreignKey: {
          name: 'prescription_itemId',
      },
      constraints: false,
    });








//end loop



    db.prescription_items.belongsTo(db.prescriptions, {
      as: 'prescription',
      foreignKey: {
        name: 'prescriptionId',
      },
      constraints: false,
    });

    db.prescription_items.belongsTo(db.medicines, {
      as: 'medicine',
      foreignKey: {
        name: 'medicineId',
      },
      constraints: false,
    });

    db.prescription_items.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return prescription_items;
};


