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

exportType: {
        type: DataTypes.TEXT,
      
      

      },

filtersJson: {
        type: DataTypes.TEXT,
      
      

      },

recordCount: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"PENDING",


"APPROVED",


"REJECTED",


"CANCELLED",


"EXPIRED"

        ],

      },

approvedAt: {
        type: DataTypes.DATE,
      
      

      },

expiresAt: {
        type: DataTypes.DATE,
      
      

      },

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

  export_requests.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.export_requests.belongsTo(db.users, {
      as: 'requestedByUser',
      foreignKey: {
        name: 'requestedByUserId',
      },
      constraints: false,
    });

    db.export_requests.belongsTo(db.users, {
      as: 'approvedByUser',
      foreignKey: {
        name: 'approvedByUserId',
      },
      constraints: false,
    });



    db.export_requests.hasMany(db.file, {
      as: 'fileUrl',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.export_requests.getTableName(),
        belongsToColumn: 'fileUrl',
      },
    });


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

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



  return export_requests;
};


