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,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

encrypted_key: {
        type: DataTypes.TEXT,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

is_active: {
        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









    db.api_keys.hasMany(db.fill_sessions, {
      as: 'fill_sessions_api_key',
      foreignKey: {
          name: 'api_keyId',
      },
      constraints: false,
    });







//end loop



    db.api_keys.belongsTo(db.ai_models, {
      as: 'model',
      foreignKey: {
        name: 'modelId',
      },
      constraints: false,
    });

    db.api_keys.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });




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

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



  return api_keys;
};


