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

display_name: {
        type: DataTypes.TEXT,
      
      

      },

provider: {
        type: DataTypes.TEXT,
      
      

      },

model_key: {
        type: DataTypes.TEXT,
      
      

      },

capability_class: {
        type: DataTypes.ENUM,
      
      

        values: [

"chat",


"code",


"vision",


"tools",


"reasoning"

        ],

      },

max_context_tokens: {
        type: DataTypes.INTEGER,
      
      

      },

input_cost_per_1k: {
        type: DataTypes.DECIMAL,
      
      

      },

output_cost_per_1k: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

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

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"deprecated",


"disabled"

        ],

      },

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

  ai_models.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_models.hasMany(db.ai_agents, {
      as: 'ai_agents_default_model',
      foreignKey: {
          name: 'default_modelId',
      },
      constraints: false,
    });
















    db.ai_models.hasMany(db.usage_events, {
      as: 'usage_events_model',
      foreignKey: {
          name: 'modelId',
      },
      constraints: false,
    });




//end loop



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




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

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



  return ai_models;
};


