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

region_name: {
        type: DataTypes.TEXT,
      
      

      },

slug: {
        type: DataTypes.TEXT,
      
      

      },

flavor_profile: {
        type: DataTypes.TEXT,
      
      

      },

altitude_min_masl: {
        type: DataTypes.INTEGER,
      
      

      },

altitude_max_masl: {
        type: DataTypes.INTEGER,
      
      

      },

rainfall_note: {
        type: DataTypes.TEXT,
      
      

      },

story: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

sort_order: {
        type: DataTypes.INTEGER,
      
      

      },

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

  coffee_regions.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_regions.hasMany(db.coffee_lots, {
      as: 'coffee_lots_region',
      foreignKey: {
          name: 'regionId',
      },
      constraints: false,
    });









    db.coffee_regions.hasMany(db.inquiries, {
      as: 'inquiries_requested_region',
      foreignKey: {
          name: 'requested_regionId',
      },
      constraints: false,
    });





//end loop





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


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

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



  return coffee_regions;
};


