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

sequence_number: {
        type: DataTypes.INTEGER,
      
      

      },

step_name: {
        type: DataTypes.TEXT,
      
      

      },

setup_time_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

run_time_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

work_instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  routing_steps.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_steps.belongsTo(db.routings, {
      as: 'routing',
      foreignKey: {
        name: 'routingId',
      },
      constraints: false,
    });

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



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


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

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



  return routing_steps;
};


