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_time: {
        type: DataTypes.DATE,
      
      

      },

action: {
        type: DataTypes.ENUM,
      
      

        values: [

"create",


"update",


"delete",


"approve",


"reject",


"export",


"import",


"login",


"logout",


"download"

        ],

      },

entity_type: {
        type: DataTypes.TEXT,
      
      

      },

entity_key: {
        type: DataTypes.TEXT,
      
      

      },

old_value_json: {
        type: DataTypes.TEXT,
      
      

      },

new_value_json: {
        type: DataTypes.TEXT,
      
      

      },

change_summary: {
        type: DataTypes.TEXT,
      
      

      },

ip_address: {
        type: DataTypes.TEXT,
      
      

      },

user_agent: {
        type: DataTypes.TEXT,
      
      

      },

session_key: {
        type: DataTypes.TEXT,
      
      

      },

checksum: {
        type: DataTypes.TEXT,
      
      

      },

previous_checksum: {
        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.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.audit_logs.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.audit_logs.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });




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

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



  return audit_logs;
};


