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

medicine_name: {
        type: DataTypes.TEXT,
      
      

      },

generic_name: {
        type: DataTypes.TEXT,
      
      

      },

strength: {
        type: DataTypes.TEXT,
      
      

      },

dosage_form: {
        type: DataTypes.ENUM,
      
      

        values: [

"tablet",


"capsule",


"syrup",


"injection",


"ointment",


"drops",


"inhaler",


"other"

        ],

      },

manufacturer: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

storage_instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  medicines.associate = (db) => {


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













    db.medicines.hasMany(db.inventory_items, {
      as: 'inventory_items_medicine',
      foreignKey: {
          name: 'medicineId',
      },
      constraints: false,
    });



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












//end loop



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




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

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



  return medicines;
};


