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

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

center_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"machine",


"assembly",


"inspection",


"packaging",


"outsourced"

        ],

      },

default_setup_minutes: {
        type: DataTypes.DECIMAL,
      
      

      },

default_run_rate_per_hour: {
        type: DataTypes.DECIMAL,
      
      

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  work_centers.associate = (db) => {


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


















    db.work_centers.hasMany(db.machines, {
      as: 'machines_work_center',
      foreignKey: {
          name: 'work_centerId',
      },
      constraints: false,
    });



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



    db.work_centers.hasMany(db.production_operations, {
      as: 'production_operations_work_center',
      foreignKey: {
          name: 'work_centerId',
      },
      constraints: false,
    });























//end loop






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

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



  return work_centers;
};


