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,
      
      

      },

material_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"raw",


"component",


"packaging",


"finished_good",


"consumable"

        ],

      },

standard_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

standard_lead_time_days: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

reorder_point: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_quantity: {
        type: DataTypes.DECIMAL,
      
      

      },

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_lots, {
      as: 'inventory_lots_material',
      foreignKey: {
          name: 'materialId',
      },
      constraints: false,
    });


    db.materials.hasMany(db.inventory_balances, {
      as: 'inventory_balances_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.boms, {
      as: 'boms_parent_material',
      foreignKey: {
          name: 'parent_materialId',
      },
      constraints: false,
    });


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


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



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



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



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





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





//end loop



    db.materials.belongsTo(db.units_of_measure, {
      as: 'uom',
      foreignKey: {
        name: 'uomId',
      },
      constraints: false,
    });

    db.materials.belongsTo(db.suppliers, {
      as: 'preferred_supplier',
      foreignKey: {
        name: 'preferred_supplierId',
      },
      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;
};


