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

issue_number: {
        type: DataTypes.TEXT,
      
      

      },

issued_at: {
        type: DataTypes.DATE,
      
      

      },

issue_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"manual_pick",


"backflush",


"return"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  material_issues.associate = (db) => {


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

























    db.material_issues.hasMany(db.material_issue_lines, {
      as: 'material_issue_lines_material_issue',
      foreignKey: {
          name: 'material_issueId',
      },
      constraints: false,
    });













//end loop



    db.material_issues.belongsTo(db.work_orders, {
      as: 'work_order',
      foreignKey: {
        name: 'work_orderId',
      },
      constraints: false,
    });

    db.material_issues.belongsTo(db.users, {
      as: 'issued_by',
      foreignKey: {
        name: 'issued_byId',
      },
      constraints: false,
    });



    db.material_issues.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.material_issues.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return material_issues;
};


