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,
      
      

      },

timezone: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

address_line1: {
        type: DataTypes.TEXT,
      
      

      },

address_line2: {
        type: DataTypes.TEXT,
      
      

      },

city: {
        type: DataTypes.TEXT,
      
      

      },

state_region: {
        type: DataTypes.TEXT,
      
      

      },

postal_code: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.TEXT,
      
      

      },

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




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


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


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



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







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



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


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



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



//end loop



    db.plants.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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


