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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"succeeded",


"failed",


"blocked"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

prompt_tokens: {
        type: DataTypes.INTEGER,
      
      

      },

completion_tokens: {
        type: DataTypes.INTEGER,
      
      

      },

estimated_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

model_used: {
        type: DataTypes.TEXT,
      
      

      },

error_message: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  ai_requests.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity





















    db.ai_requests.hasMany(db.ai_outputs, {
      as: 'ai_outputs_ai_request',
      foreignKey: {
          name: 'ai_requestId',
      },
      constraints: false,
    });



    db.ai_requests.hasMany(db.messages, {
      as: 'messages_ai_request',
      foreignKey: {
          name: 'ai_requestId',
      },
      constraints: false,
    });






//end loop



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

    db.ai_requests.belongsTo(db.ai_tasks, {
      as: 'ai_task',
      foreignKey: {
        name: 'ai_taskId',
      },
      constraints: false,
    });

    db.ai_requests.belongsTo(db.ai_gateways, {
      as: 'ai_gateway',
      foreignKey: {
        name: 'ai_gatewayId',
      },
      constraints: false,
    });




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

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



  return ai_requests;
};


