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

alert_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"inventory_low",


"machine_down",


"qa_failure",


"maintenance_overdue",


"order_late",


"temperature_high",


"certificate_missing"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"info",


"warning",


"critical"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"acknowledged",


"resolved"

        ],

      },

title_text: {
        type: DataTypes.TEXT,
      
      

      },

message: {
        type: DataTypes.TEXT,
      
      

      },

triggered_at: {
        type: DataTypes.DATE,
      
      

      },

acknowledged_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  alerts.associate = (db) => {


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




































//end loop



    db.alerts.belongsTo(db.users, {
      as: 'acknowledged_by_user',
      foreignKey: {
        name: 'acknowledged_by_userId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.plants, {
      as: 'related_plant',
      foreignKey: {
        name: 'related_plantId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.machines, {
      as: 'related_machine',
      foreignKey: {
        name: 'related_machineId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.production_orders, {
      as: 'related_order',
      foreignKey: {
        name: 'related_orderId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.inventory_lots, {
      as: 'related_lot',
      foreignKey: {
        name: 'related_lotId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.inspections, {
      as: 'related_inspection',
      foreignKey: {
        name: 'related_inspectionId',
      },
      constraints: false,
    });




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

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



  return alerts;
};


