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_no: {
        type: DataTypes.INTEGER,
      
      

      },

operation_name: {
        type: DataTypes.TEXT,
      
      

      },

setup_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

run_minutes_per_unit: {
        type: DataTypes.DECIMAL,
      
      

      },

work_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





































//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: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.routing_operations.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return routing_operations;
};


