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

recorded_at: {
        type: DataTypes.DATE,
      
      

      },

latitude: {
        type: DataTypes.DECIMAL,
      
      

      },

longitude: {
        type: DataTypes.DECIMAL,
      
      

      },

speed: {
        type: DataTypes.DECIMAL,
      
      

      },

accuracy_meters: {
        type: DataTypes.DECIMAL,
      
      

      },

battery_level: {
        type: DataTypes.DECIMAL,
      
      

      },

event_kind: {
        type: DataTypes.ENUM,
      
      

        values: [

"location_update",


"geofence_enter",


"geofence_exit",


"panic",


"fall_suspected",


"device_offline"

        ],

      },

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

  iot_telemetry_events.associate = (db) => {


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





















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









//end loop



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

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

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




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

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



  return iot_telemetry_events;
};


