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

event_at: {
        type: DataTypes.DATE,
      
      

      },

event_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"create",


"update",


"delete",


"view",


"export",


"login",


"logout",


"approve",


"reject",


"esign",


"assign",


"upload",


"download"

        ],

      },

subject_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"document",


"document_version",


"document_template",


"change_request",


"training_assignment",


"risk_assessment",


"risk_item",


"capa",


"nonconformance",


"complaint",


"audit",


"audit_finding",


"management_review",


"regulatory_submission",


"supplier",


"product",


"batch",


"coa",


"dmr",


"user",


"other"

        ],

      },

subject_reference: {
        type: DataTypes.TEXT,
      
      

      },

details: {
        type: DataTypes.TEXT,
      
      

      },

ip_address: {
        type: DataTypes.TEXT,
      
      

      },

user_agent: {
        type: DataTypes.TEXT,
      
      

      },

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

  audit_logs.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.audit_logs.belongsTo(db.users, {
      as: 'actor',
      foreignKey: {
        name: 'actorId',
      },
      constraints: false,
    });




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

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



  return audit_logs;
};


