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

key_name: {
        type: DataTypes.TEXT,
      
      

      },

key_prefix: {
        type: DataTypes.TEXT,
      
      

      },

access_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"read",


"write",


"admin"

        ],

      },

last_used_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

is_revoked: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

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

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




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

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



  return api_keys;
};


