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

action: {
        type: DataTypes.ENUM,
      
      

        values: [

"view",


"download",


"export_zip",


"search"

        ],

      },

resource_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"archive_item",


"requisition",


"purchase_order",


"supplier",


"audit_pack"

        ],

      },

resource_reference: {
        type: DataTypes.TEXT,
      
      

      },

ip_address: {
        type: DataTypes.TEXT,
      
      

      },

user_agent: {
        type: DataTypes.TEXT,
      
      

      },

occurred_at: {
        type: DataTypes.DATE,
      
      

      },

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

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

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

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




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

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



  return access_logs;
};


