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

name: {
        type: DataTypes.TEXT,
      
      

      },

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

  organizations.associate = (db) => {


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


    db.organizations.hasMany(db.users, {
      as: 'users_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });





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


    db.organizations.hasMany(db.data_sources, {
      as: 'data_sources_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.ingestion_pipelines, {
      as: 'ingestion_pipelines_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


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


    db.organizations.hasMany(db.ai_models, {
      as: 'ai_models_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.detections, {
      as: 'detections_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.incidents, {
      as: 'incidents_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.response_playbooks, {
      as: 'response_playbooks_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.response_actions, {
      as: 'response_actions_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.soc_agents, {
      as: 'soc_agents_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.threat_actors, {
      as: 'threat_actors_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.threat_indicators, {
      as: 'threat_indicators_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.threat_campaigns, {
      as: 'threat_campaigns_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.attack_map_events, {
      as: 'attack_map_events_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.risk_assessments, {
      as: 'risk_assessments_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.simulation_labs, {
      as: 'simulation_labs_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.simulation_runs, {
      as: 'simulation_runs_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.infrastructure_clusters, {
      as: 'infrastructure_clusters_organizations',
      foreignKey: {
          name: 'organizationsId',
      },
      constraints: false,
    });


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


    db.organizations.hasMany(db.reports, {
      as: 'reports_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.ai_recommendations, {
      as: 'ai_recommendations_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });



//end loop






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

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



  return organizations;
};


