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

reason_name: {
        type: DataTypes.TEXT,
      
      

      },

reason_code: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"downtime",


"scrap",


"quality",


"inventory_adjustment",


"maintenance"

        ],

      },

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

      },

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

  reason_codes.associate = (db) => {


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


















    db.reason_codes.hasMany(db.machine_events, {
      as: 'machine_events_reason_code',
      foreignKey: {
          name: 'reason_codeId',
      },
      constraints: false,
    });








    db.reason_codes.hasMany(db.inventory_movements, {
      as: 'inventory_movements_reason_code',
      foreignKey: {
          name: 'reason_codeId',
      },
      constraints: false,
    });













//end loop






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

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



  return reason_codes;
};


