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

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"info",


"warning",


"critical"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

message: {
        type: DataTypes.TEXT,
      
      

      },

trigger_at: {
        type: DataTypes.DATE,
      
      

      },

sent_at: {
        type: DataTypes.DATE,
      
      

      },

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"dashboard",


"email",


"sms",


"whatsapp"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"scheduled",


"sent",


"failed",


"cancelled"

        ],

      },

escalation_level: {
        type: DataTypes.INTEGER,
      
      

      },

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

  alerts.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.alerts.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.company_compliances, {
      as: 'related_compliance',
      foreignKey: {
        name: 'related_complianceId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.tasks, {
      as: 'related_task',
      foreignKey: {
        name: 'related_taskId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.users, {
      as: 'recipient_user',
      foreignKey: {
        name: 'recipient_userId',
      },
      constraints: false,
    });

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




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

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



  return alerts;
};


