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

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"excel",


"pdf",


"zip",


"audit_pack"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"completed",


"failed"

        ],

      },

parameters_json: {
        type: DataTypes.TEXT,
      
      

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  export_jobs.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_jobs.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.export_jobs.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

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



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


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

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



  return export_jobs;
};


