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,
      },

type: {
        type: DataTypes.ENUM,
      
      

        values: [

"low_stock",


"high_mortality",


"vaccination_due",


"overdue_invoice",


"pending_delivery",


"other"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"info",


"warning",


"critical"

        ],

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

message: {
        type: DataTypes.TEXT,
      
      

      },

triggered_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"acknowledged",


"resolved",


"dismissed"

        ],

      },

      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.clients, {
      as: 'client',
      foreignKey: {
        name: 'clientId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });

    db.alerts.belongsTo(db.poultry_lots, {
      as: 'poultry_lot',
      foreignKey: {
        name: 'poultry_lotId',
      },
      constraints: false,
    });

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




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

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



  return alerts;
};


