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

batch_code: {
        type: DataTypes.TEXT,
      
      

      },

stocked_at: {
        type: DataTypes.DATE,
      
      

      },

initial_count: {
        type: DataTypes.INTEGER,
      
      

      },

initial_avg_weight_g: {
        type: DataTypes.DECIMAL,
      
      

      },

batch_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"planned",


"active",


"harvested",


"closed",


"lost"

        ],

      },

expected_harvest_at: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  batches.associate = (db) => {


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














    db.batches.hasMany(db.inventory_movements, {
      as: 'inventory_movements_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });


    db.batches.hasMany(db.feeding_logs, {
      as: 'feeding_logs_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });



    db.batches.hasMany(db.health_events, {
      as: 'health_events_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });


    db.batches.hasMany(db.harvests, {
      as: 'harvests_batch',
      foreignKey: {
          name: 'batchId',
      },
      constraints: false,
    });















//end loop



    db.batches.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.batches.belongsTo(db.ponds, {
      as: 'pond',
      foreignKey: {
        name: 'pondId',
      },
      constraints: false,
    });

    db.batches.belongsTo(db.species, {
      as: 'species',
      foreignKey: {
        name: 'speciesId',
      },
      constraints: false,
    });

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




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

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



  return batches;
};


