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,
      },

link_name: {
        type: DataTypes.TEXT,
      
      

      },

code: {
        type: DataTypes.TEXT,
      
      

      },

destination_url: {
        type: DataTypes.TEXT,
      
      

      },

link_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"storefront",


"product",


"landing_page",


"campaign"

        ],

      },

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

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

click_count: {
        type: DataTypes.INTEGER,
      
      

      },

conversion_count: {
        type: DataTypes.INTEGER,
      
      

      },

      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




























//end loop



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

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

    db.referral_links.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      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;
};


