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

routing_code: {
        type: DataTypes.TEXT,
      
      

      },

revision: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"obsolete"

        ],

      },

effective_from: {
        type: DataTypes.DATE,
      
      

      },

effective_to: {
        type: DataTypes.DATE,
      
      

      },

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

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  routing_headers.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_headers.hasMany(db.routing_operations, {
      as: 'routing_operations_routing_header',
      foreignKey: {
          name: 'routing_headerId',
      },
      constraints: false,
    });


    db.routing_headers.hasMany(db.production_orders, {
      as: 'production_orders_routing',
      foreignKey: {
          name: 'routingId',
      },
      constraints: false,
    });
























//end loop



    db.routing_headers.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

    db.routing_headers.belongsTo(db.users, {
      as: 'approved_by',
      foreignKey: {
        name: 'approved_byId',
      },
      constraints: false,
    });




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

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



  return routing_headers;
};


