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

code: {
        type: DataTypes.TEXT,
      
      

      },

discount_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"percent",


"fixed_amount"

        ],

      },

discount_value: {
        type: DataTypes.DECIMAL,
      
      

      },

min_subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

usage_limit: {
        type: DataTypes.INTEGER,
      
      

      },

usage_count: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  promo_codes.associate = (db) => {


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















    db.promo_codes.hasMany(db.bookings, {
      as: 'bookings_promo_code',
      foreignKey: {
          name: 'promo_codeId',
      },
      constraints: false,
    });












//end loop



    db.promo_codes.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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




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

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



  return promo_codes;
};


