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

applies_to_role: {
        type: DataTypes.ENUM,
      
      

        values: [

"reseller",


"agent",


"distributor",


"master_distributor"

        ],

      },

commission_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"percentage",


"fixed_amount"

        ],

      },

rate: {
        type: DataTypes.DECIMAL,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

min_order_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

rule_name: {
        type: DataTypes.TEXT,
      
      

      },

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

  commission_rules.associate = (db) => {


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




























//end loop



    db.commission_rules.belongsTo(db.commission_plans, {
      as: 'commission_plan',
      foreignKey: {
        name: 'commission_planId',
      },
      constraints: false,
    });

    db.commission_rules.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });

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




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

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



  return commission_rules;
};


