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_type: {
        type: DataTypes.TEXT,
      
      

      },

event_reference: {
        type: DataTypes.TEXT,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

delivery_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"succeeded",


"failed"

        ],

      },

http_status: {
        type: DataTypes.INTEGER,
      
      

      },

request_payload: {
        type: DataTypes.TEXT,
      
      

      },

response_body: {
        type: DataTypes.TEXT,
      
      

      },

attempt_count: {
        type: DataTypes.INTEGER,
      
      

      },

      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: 'endpoint',
      foreignKey: {
        name: '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;
};


