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,
      
      

      },

queued_at: {
        type: DataTypes.DATE,
      
      

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"success",


"failed",


"retrying"

        ],

      },

attempts: {
        type: DataTypes.INTEGER,
      
      

      },

http_status: {
        type: DataTypes.INTEGER,
      
      

      },

request_body: {
        type: DataTypes.TEXT,
      
      

      },

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

    db.webhook_deliveries.belongsTo(db.webhooks, {
      as: 'webhook',
      foreignKey: {
        name: 'webhookId',
      },
      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;
};


