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

operation_name: {
        type: DataTypes.TEXT,
      
      

      },

operation_code: {
        type: DataTypes.TEXT,
      
      

      },

sequence: {
        type: DataTypes.INTEGER,
      
      

      },

setup_time_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

run_time_minutes_per_unit: {
        type: DataTypes.DECIMAL,
      
      

      },

labor_time_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,
    },
  );

  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.operations.hasMany(db.work_order_operations, {
      as: 'work_order_operations_operation',
      foreignKey: {
          name: 'operationId',
      },
      constraints: false,
    });










    db.operations.hasMany(db.qa_inspections, {
      as: 'qa_inspections_operation',
      foreignKey: {
          name: 'operationId',
      },
      constraints: false,
    });






//end loop



    db.operations.belongsTo(db.routes, {
      as: 'route',
      foreignKey: {
        name: 'routeId',
      },
      constraints: false,
    });



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


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

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



  return operations;
};


