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,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

url: {
        type: DataTypes.TEXT,
      
      

      },

secret: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"disabled"

        ],

      },

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

      },

last_success_at: {
        type: DataTypes.DATE,
      
      

      },

last_failure_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_webhook_endpoint',
      foreignKey: {
          name: 'webhook_endpointId',
      },
      constraints: false,
    });





//end loop



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




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

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



  return webhook_endpoints;
};


