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

operation_number: {
        type: DataTypes.INTEGER,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

setup_time_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

run_time_minutes_per_unit: {
        type: DataTypes.DECIMAL,
      
      

      },

move_time_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

queue_time_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  routing_operations.associate = (db) => {


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




















    db.routing_operations.hasMany(db.production_operations, {
      as: 'production_operations_routing_operation',
      foreignKey: {
          name: 'routing_operationId',
      },
      constraints: false,
    });













//end loop



    db.routing_operations.belongsTo(db.routings, {
      as: 'routing',
      foreignKey: {
        name: 'routingId',
      },
      constraints: false,
    });

    db.routing_operations.belongsTo(db.work_centers, {
      as: 'work_center',
      foreignKey: {
        name: 'work_centerId',
      },
      constraints: false,
    });



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


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

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



  return routing_operations;
};


