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

event_key: {
        type: DataTypes.TEXT,
      
      

      },

payload_json: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"delivered",


"failed"

        ],

      },

attempt_count: {
        type: DataTypes.INTEGER,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

next_retry_at: {
        type: DataTypes.DATE,
      
      

      },

last_error: {
        type: DataTypes.TEXT,
      
      

      },

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

  webhook_deliveries.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.webhook_deliveries.belongsTo(db.webhook_endpoints, {
      as: 'webhook_endpoint',
      foreignKey: {
        name: 'webhook_endpointId',
      },
      constraints: false,
    });

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




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

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



  return webhook_deliveries;
};


