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

anomaly_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"amount_spike",


"duplicate",


"new_vendor",


"category_shift",


"cash_drain",


"unusual_frequency",


"currency_mismatch"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

anomaly_score: {
        type: DataTypes.DECIMAL,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

explanation: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"acknowledged",


"resolved",


"dismissed"

        ],

      },

detected_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  anomalies.associate = (db) => {


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



















    db.anomalies.hasMany(db.alerts, {
      as: 'alerts_related_anomaly',
      foreignKey: {
          name: 'related_anomalyId',
      },
      constraints: false,
    });









//end loop



    db.anomalies.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.anomalies.belongsTo(db.transactions, {
      as: 'transaction',
      foreignKey: {
        name: 'transactionId',
      },
      constraints: false,
    });

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




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

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



  return anomalies;
};


