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

plant_name: {
        type: DataTypes.TEXT,
      
      

      },

plant_code: {
        type: DataTypes.TEXT,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

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

  plants.associate = (db) => {


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








    db.plants.hasMany(db.warehouses, {
      as: 'warehouses_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });










    db.plants.hasMany(db.work_centers, {
      as: 'work_centers_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });





    db.plants.hasMany(db.shifts, {
      as: 'shifts_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });


    db.plants.hasMany(db.production_orders, {
      as: 'production_orders_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });





    db.plants.hasMany(db.purchase_orders, {
      as: 'purchase_orders_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });








    db.plants.hasMany(db.alerts, {
      as: 'alerts_related_plant',
      foreignKey: {
          name: 'related_plantId',
      },
      constraints: false,
    });




//end loop






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

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



  return plants;
};


