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

observed_at: {
        type: DataTypes.DATE,
      
      

      },

event_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"disease",


"mortality",


"treatment",


"inspection",


"biosecurity",


"other"

        ],

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

mortality_count: {
        type: DataTypes.INTEGER,
      
      

      },

symptoms: {
        type: DataTypes.TEXT,
      
      

      },

treatment: {
        type: DataTypes.TEXT,
      
      

      },

resolution_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"monitoring",


"resolved",


"closed"

        ],

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  health_events.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.health_events.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.health_events.belongsTo(db.batches, {
      as: 'batch',
      foreignKey: {
        name: 'batchId',
      },
      constraints: false,
    });

    db.health_events.belongsTo(db.users, {
      as: 'reported_by',
      foreignKey: {
        name: 'reported_byId',
      },
      constraints: false,
    });

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



    db.health_events.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.health_events.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return health_events;
};


