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

code: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

discount_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"percentage",


"fixed_amount"

        ],

      },

discount_value: {
        type: DataTypes.DECIMAL,
      
      

      },

min_order_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

usage_limit: {
        type: DataTypes.INTEGER,
      
      

      },

usage_count: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

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

  coupons.associate = (db) => {


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






















    db.coupons.hasMany(db.coupon_redemptions, {
      as: 'coupon_redemptions_coupon',
      foreignKey: {
          name: 'couponId',
      },
      constraints: false,
    });









//end loop






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

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



  return coupons;
};


