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

name: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"online",


"offline",


"degraded"

        ],

      },

location: {
        type: DataTypes.TEXT,
      
      

      },

site: {
        type: DataTypes.TEXT,
      
      

      },

hardware_model: {
        type: DataTypes.TEXT,
      
      

      },

firmware_version: {
        type: DataTypes.TEXT,
      
      

      },

last_seen_at: {
        type: DataTypes.DATE,
      
      

      },

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

  edge_sensors.associate = (db) => {


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






    db.edge_sensors.hasMany(db.assets, {
      as: 'assets_sensor',
      foreignKey: {
          name: 'sensorId',
      },
      constraints: false,
    });























//end loop



    db.edge_sensors.belongsTo(db.infrastructure_clusters, {
      as: 'cluster',
      foreignKey: {
        name: 'clusterId',
      },
      constraints: false,
    });

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




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

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



  return edge_sensors;
};


