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

plan_name: {
        type: DataTypes.TEXT,
      
      

      },

plan_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"inactive"

        ],

      },

calculation_base: {
        type: DataTypes.ENUM,
      
      

        values: [

"order_subtotal",


"order_total",


"profit_estimate",


"line_item_total"

        ],

      },

commission_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"percentage",


"fixed_amount"

        ],

      },

default_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

default_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

effective_from: {
        type: DataTypes.DATE,
      
      

      },

effective_to: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  commission_plans.associate = (db) => {


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

















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


    db.commission_plans.hasMany(db.commissions, {
      as: 'commissions_commission_plan',
      foreignKey: {
          name: 'commission_planId',
      },
      constraints: false,
    });











//end loop



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

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




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

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



  return commission_plans;
};


