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

product_name: {
        type: DataTypes.TEXT,
      
      

      },

brand: {
        type: DataTypes.TEXT,
      
      

      },

feed_type: {
        type: DataTypes.TEXT,
      
      

      },

protein_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

fat_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

pellet_size_mm: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  feed_products.associate = (db) => {


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













    db.feed_products.hasMany(db.inventory_items, {
      as: 'inventory_items_feed_product',
      foreignKey: {
          name: 'feed_productId',
      },
      constraints: false,
    });



    db.feed_products.hasMany(db.feeding_logs, {
      as: 'feeding_logs_feed_product',
      foreignKey: {
          name: 'feed_productId',
      },
      constraints: false,
    });


















//end loop



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

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




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

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



  return feed_products;
};


