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

endpoint_name: {
        type: DataTypes.TEXT,
      
      

      },

url: {
        type: DataTypes.TEXT,
      
      

      },

secret: {
        type: DataTypes.TEXT,
      
      

      },

is_enabled: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

event_types: {
        type: DataTypes.TEXT,
      
      

      },

last_delivery_at: {
        type: DataTypes.DATE,
      
      

      },

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

  webhook_endpoints.associate = (db) => {


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



























    db.webhook_endpoints.hasMany(db.webhook_deliveries, {
      as: 'webhook_deliveries_endpoint',
      foreignKey: {
          name: 'endpointId',
      },
      constraints: false,
    });






//end loop



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

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




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

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



  return webhook_endpoints;
};


