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,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

      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.work_centers, {
      as: 'work_centers_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });










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



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


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




    db.plants.hasMany(db.cycle_counts, {
      as: 'cycle_counts_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.production_lots, {
      as: 'production_lots_plant',
      foreignKey: {
          name: 'plantId',
      },
      constraints: false,
    });













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




//end loop



    db.plants.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });




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

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



  return plants;
};


