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,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"tablet",


"capsule",


"syrup",


"injection",


"ointment",


"drops",


"device",


"other"

        ],

      },

strength: {
        type: DataTypes.TEXT,
      
      

      },

unit: {
        type: DataTypes.TEXT,
      
      

      },

manufacturer: {
        type: DataTypes.TEXT,
      
      

      },

unit_price: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

reorder_level: {
        type: DataTypes.INTEGER,
      
      

      },

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_updates, {
      as: 'inventory_updates_medicine',
      foreignKey: {
          name: 'medicineId',
      },
      constraints: false,
    });



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















//end loop






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

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



  return medicines;
};


