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

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"closed",


"quarantine"

        ],

      },

initial_count: {
        type: DataTypes.INTEGER,
      
      

      },

current_count: {
        type: DataTypes.INTEGER,
      
      

      },

deaths_count: {
        type: DataTypes.INTEGER,
      
      

      },

mortality_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

housing_location: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  poultry_lots.associate = (db) => {


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





















    db.poultry_lots.hasMany(db.egg_collections, {
      as: 'egg_collections_poultry_lot',
      foreignKey: {
          name: 'poultry_lotId',
      },
      constraints: false,
    });


    db.poultry_lots.hasMany(db.treatments, {
      as: 'treatments_poultry_lot',
      foreignKey: {
          name: 'poultry_lotId',
      },
      constraints: false,
    });


    db.poultry_lots.hasMany(db.alerts, {
      as: 'alerts_poultry_lot',
      foreignKey: {
          name: 'poultry_lotId',
      },
      constraints: false,
    });





//end loop



    db.poultry_lots.belongsTo(db.users, {
      as: 'manager_user',
      foreignKey: {
        name: 'manager_userId',
      },
      constraints: false,
    });




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

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



  return poultry_lots;
};


