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

integration_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"jira",


"asana",


"google_calendar",


"ms_graph",


"slack",


"email_smtp",


"webhook"

        ],

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

integration_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"disabled",


"error"

        ],

      },

auth_reference: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

last_sync_at: {
        type: DataTypes.DATE,
      
      

      },

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

  external_integrations.associate = (db) => {


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




































    db.external_integrations.hasMany(db.tasks, {
      as: 'tasks_external_integration',
      foreignKey: {
          name: 'external_integrationId',
      },
      constraints: false,
    });




















//end loop



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

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




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

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



  return external_integrations;
};


