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

lot_code: {
        type: DataTypes.TEXT,
      
      

      },

process_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"washed",


"natural",


"honey"

        ],

      },

grade: {
        type: DataTypes.ENUM,
      
      

        values: [

"grade_1",


"grade_2",


"grade_3",


"grade_4",


"grade_5",


"commercial"

        ],

      },

crop_year: {
        type: DataTypes.INTEGER,
      
      

      },

moisture_percent: {
        type: DataTypes.DECIMAL,
      
      

      },

tasting_notes: {
        type: DataTypes.TEXT,
      
      

      },

scaa_score: {
        type: DataTypes.DECIMAL,
      
      

      },

certifications: {
        type: DataTypes.TEXT,
      
      

      },

available_quantity_kg: {
        type: DataTypes.DECIMAL,
      
      

      },

is_available: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  coffee_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.coffee_lots.hasMany(db.inquiries, {
      as: 'inquiries_requested_lot',
      foreignKey: {
          name: 'requested_lotId',
      },
      constraints: false,
    });





//end loop



    db.coffee_lots.belongsTo(db.coffee_regions, {
      as: 'region',
      foreignKey: {
        name: 'regionId',
      },
      constraints: false,
    });



    db.coffee_lots.hasMany(db.file, {
      as: 'images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.coffee_lots.getTableName(),
        belongsToColumn: 'images',
      },
    });


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

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



  return coffee_lots;
};


