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

route_name: {
        type: DataTypes.TEXT,
      
      

      },

route_code: {
        type: DataTypes.TEXT,
      
      

      },

route_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"obsolete"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  routes.associate = (db) => {


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

















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






    db.routes.hasMany(db.work_orders, {
      as: 'work_orders_route',
      foreignKey: {
          name: 'routeId',
      },
      constraints: false,
    });
















//end loop






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

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



  return routes;
};


