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

job_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"approval_reminder",


"upcoming_check_in_reminder",


"overdue_invoice_notice",


"upcoming_check_out_reminder",


"daily_digest",


"system_cleanup"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"succeeded",


"failed",


"canceled"

        ],

      },

scheduled_for: {
        type: DataTypes.DATE,
      
      

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

finished_at: {
        type: DataTypes.DATE,
      
      

      },

attempt_count: {
        type: DataTypes.INTEGER,
      
      

      },

last_error: {
        type: DataTypes.TEXT,
      
      

      },

payload_json: {
        type: DataTypes.TEXT,
      
      

      },

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

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

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




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

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



  return job_runs;
};


