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

device_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"gps_bracelet",


"phone",


"beacon",


"other"

        ],

      },

device_label: {
        type: DataTypes.TEXT,
      
      

      },

serial_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive",


"lost",


"maintenance"

        ],

      },

paired_at: {
        type: DataTypes.DATE,
      
      

      },

last_seen_at: {
        type: DataTypes.DATE,
      
      

      },

battery_level: {
        type: DataTypes.DECIMAL,
      
      

      },

firmware_version: {
        type: DataTypes.TEXT,
      
      

      },

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

  iot_devices.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_devices.hasMany(db.iot_telemetry_events, {
      as: 'iot_telemetry_events_device',
      foreignKey: {
          name: 'deviceId',
      },
      constraints: false,
    });



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









//end loop



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

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




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

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



  return iot_devices;
};


