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

alert_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"wandering",


"geofence_breach",


"panic",


"fall_suspected",


"missed_medication",


"device_offline",


"other"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

title_text: {
        type: DataTypes.TEXT,
      
      

      },

details: {
        type: DataTypes.TEXT,
      
      

      },

triggered_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"acknowledged",


"resolved",


"dismissed"

        ],

      },

acknowledged_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

      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






















    db.alerts.hasMany(db.alert_notifications, {
      as: 'alert_notifications_alert',
      foreignKey: {
          name: 'alertId',
      },
      constraints: false,
    });








//end loop



    db.alerts.belongsTo(db.patients, {
      as: 'patient',
      foreignKey: {
        name: 'patientId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.iot_devices, {
      as: 'device',
      foreignKey: {
        name: 'deviceId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.iot_telemetry_events, {
      as: 'trigger_event',
      foreignKey: {
        name: 'trigger_eventId',
      },
      constraints: false,
    });

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

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




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

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



  return alerts;
};


