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

species_name: {
        type: DataTypes.TEXT,
      
      

      },

scientific_name: {
        type: DataTypes.TEXT,
      
      

      },

typical_harvest_size: {
        type: DataTypes.TEXT,
      
      

      },

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

  species.associate = (db) => {


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











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









    db.species.hasMany(db.marketplace_listings, {
      as: 'marketplace_listings_species',
      foreignKey: {
          name: 'speciesId',
      },
      constraints: false,
    });














//end loop



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




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

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



  return species;
};


