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

code: {
        type: DataTypes.TEXT,
      
      

      },

landing_url: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

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

  referral_links.associate = (db) => {


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










































    db.referral_links.hasMany(db.referral_conversions, {
      as: 'referral_conversions_referral_link',
      foreignKey: {
          name: 'referral_linkId',
      },
      constraints: false,
    });



//end loop



    db.referral_links.belongsTo(db.users, {
      as: 'referrer_user',
      foreignKey: {
        name: 'referrer_userId',
      },
      constraints: false,
    });

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

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




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

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



  return referral_links;
};


