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

name: {
        type: DataTypes.TEXT,
      
      

      },

target_url: {
        type: DataTypes.TEXT,
      
      

      },

secret: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"disabled"

        ],

      },

event_types: {
        type: DataTypes.TEXT,
      
      

      },

timeout_seconds: {
        type: DataTypes.INTEGER,
      
      

      },

max_retries: {
        type: DataTypes.INTEGER,
      
      

      },

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

  webhooks.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
























    db.webhooks.hasMany(db.webhook_deliveries, {
      as: 'webhook_deliveries_webhook',
      foreignKey: {
          name: 'webhookId',
      },
      constraints: false,
    });







//end loop



    db.webhooks.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return webhooks;
};


