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

name: {
        type: DataTypes.TEXT,
      
      

      },

asset_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"host",


"server",


"workstation",


"container",


"kubernetes_cluster",


"database",


"application",


"api",


"network_device",


"iot",


"edge_sensor"

        ],

      },

environment: {
        type: DataTypes.TEXT,
      
      

      },

ip_address: {
        type: DataTypes.TEXT,
      
      

      },

hostname: {
        type: DataTypes.TEXT,
      
      

      },

os: {
        type: DataTypes.TEXT,
      
      

      },

cloud_provider: {
        type: DataTypes.TEXT,
      
      

      },

criticality: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

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

      },

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

  assets.associate = (db) => {


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









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












    db.assets.hasMany(db.risk_assessments, {
      as: 'risk_assessments_asset',
      foreignKey: {
          name: 'assetId',
      },
      constraints: false,
    });









//end loop



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

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




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

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



  return assets;
};


