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

routing_code: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"inactive"

        ],

      },

standard_batch_size: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  routings.associate = (db) => {


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
















    db.routings.hasMany(db.operations, {
      as: 'operations_routing',
      foreignKey: {
          name: 'routingId',
      },
      constraints: false,
    });







    db.routings.hasMany(db.production_orders, {
      as: 'production_orders_routing',
      foreignKey: {
          name: 'routingId',
      },
      constraints: false,
    });















//end loop



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

    db.routings.belongsTo(db.material_revisions, {
      as: 'revision',
      foreignKey: {
        name: 'revisionId',
      },
      constraints: false,
    });




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

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



  return routings;
};


