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,
      },

material_code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"lumber",


"sheet_goods",


"fasteners",


"hardware",


"adhesives",


"wraps",


"metal_connectors",


"other"

        ],

      },

uom: {
        type: DataTypes.TEXT,
      
      

      },

standard_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

last_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

sale_price: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_point: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

notes: {
        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,
    });


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



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



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




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



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



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





//end loop






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

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



  return materials;
};


