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

sku: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

material_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"raw",


"component",


"consumable",


"packaging"

        ],

      },

unit_of_measure: {
        type: DataTypes.TEXT,
      
      

      },

standard_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_point: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

specification: {
        type: DataTypes.TEXT,
      
      

      },

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

  materials.associate = (db) => {


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















    db.materials.hasMany(db.inventory_items, {
      as: 'inventory_items_material',
      foreignKey: {
          name: 'materialId',
      },
      constraints: false,
    });















//end loop



    db.materials.belongsTo(db.vendors, {
      as: 'preferred_vendor',
      foreignKey: {
        name: 'preferred_vendorId',
      },
      constraints: false,
    });

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



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


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

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



  return materials;
};


