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

observed_at: {
        type: DataTypes.DATE,
      
      

      },

event_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"auth",


"process",


"network",


"dns",


"http",


"email",


"file",


"cloud",


"kubernetes",


"iam",


"vulnerability",


"threat_feed",


"dark_web"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"informational",


"low",


"medium",


"high",


"critical"

        ],

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

raw_payload: {
        type: DataTypes.TEXT,
      
      

      },

hash: {
        type: DataTypes.TEXT,
      
      

      },

flagged: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  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.telemetry_events.hasMany(db.detections, {
      as: 'detections_event',
      foreignKey: {
          name: 'eventId',
      },
      constraints: false,
    });


















//end loop



    db.telemetry_events.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.telemetry_events.belongsTo(db.data_sources, {
      as: 'data_source',
      foreignKey: {
        name: 'data_sourceId',
      },
      constraints: false,
    });

    db.telemetry_events.belongsTo(db.assets, {
      as: 'asset',
      foreignKey: {
        name: 'assetId',
      },
      constraints: false,
    });




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

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



  return telemetry_events;
};


