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

requested_at: {
        type: DataTypes.DATE,
      
      

      },

output_format: {
        type: DataTypes.ENUM,
      
      

        values: [

"screen",


"pdf",


"excel",


"txt"

        ],

      },

filters_json: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"completed",


"failed"

        ],

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

  report_runs.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.report_runs.belongsTo(db.report_definitions, {
      as: 'report_definition',
      foreignKey: {
        name: 'report_definitionId',
      },
      constraints: false,
    });

    db.report_runs.belongsTo(db.users, {
      as: 'requested_by_user',
      foreignKey: {
        name: 'requested_by_userId',
      },
      constraints: false,
    });



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


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

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



  return report_runs;
};


