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

system_name: {
        type: DataTypes.TEXT,
      
      

      },

system_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"entra_id",


"microsoft_365_audit",


"okta",


"google_workspace",


"hris",


"ticketing_psa",


"pam",


"endpoint_mdm",


"siem",


"backup_recovery",


"custom_source"

        ],

      },

vendor: {
        type: DataTypes.TEXT,
      
      

      },

environment: {
        type: DataTypes.ENUM,
      
      

        values: [

"production",


"staging",


"sandbox",


"unknown"

        ],

      },

owner: {
        type: DataTypes.TEXT,
      
      

      },

connection_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"connected",


"degraded",


"disconnected",


"not_configured"

        ],

      },

last_successful_sync_at: {
        type: DataTypes.DATE,
      
      

      },

connection_health_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  connected_systems.associate = (db) => {


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











    db.connected_systems.hasMany(db.evidence_models, {
      as: 'evidence_models_source_system',
      foreignKey: {
          name: 'source_systemId',
      },
      constraints: false,
    });


    db.connected_systems.hasMany(db.artifacts, {
      as: 'artifacts_source_system',
      foreignKey: {
          name: 'source_systemId',
      },
      constraints: false,
    });














//end loop



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




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

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



  return connected_systems;
};


