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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"applied",


"approved",


"rejected",


"suspended",


"left"

        ],

      },

applied_at: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

referral_code: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  affiliate_memberships.associate = (db) => {


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




































    db.affiliate_memberships.hasMany(db.commissions, {
      as: 'commissions_membership',
      foreignKey: {
          name: 'membershipId',
      },
      constraints: false,
    });









//end loop



    db.affiliate_memberships.belongsTo(db.affiliate_programs, {
      as: 'program',
      foreignKey: {
        name: 'programId',
      },
      constraints: false,
    });

    db.affiliate_memberships.belongsTo(db.users, {
      as: 'member',
      foreignKey: {
        name: 'memberId',
      },
      constraints: false,
    });

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




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

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



  return affiliate_memberships;
};


