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

name: {
        type: DataTypes.TEXT,
      
      

      },

base_url: {
        type: DataTypes.TEXT,
      
      

      },

provider_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"deepseek",


"queen",


"openai_compatible",


"custom"

        ],

      },

region_policy: {
        type: DataTypes.ENUM,
      
      

        values: [

"allowed",


"blocked",


"unknown"

        ],

      },

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

      },

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

      },

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

      },

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

  llm_providers.associate = (db) => {

    db.llm_providers.belongsToMany(db.llm_api_keys, {
      as: 'api_keys',
      foreignKey: {
        name: 'llm_providers_api_keysId',
      },
      constraints: false,
      through: 'llm_providersApi_keysLlm_api_keys',
    });

    db.llm_providers.belongsToMany(db.llm_api_keys, {
      as: 'api_keys_filter',
      foreignKey: {
        name: 'llm_providers_api_keysId',
      },
      constraints: false,
      through: 'llm_providersApi_keysLlm_api_keys',
    });

    db.llm_providers.belongsToMany(db.llm_models, {
      as: 'models',
      foreignKey: {
        name: 'llm_providers_modelsId',
      },
      constraints: false,
      through: 'llm_providersModelsLlm_models',
    });

    db.llm_providers.belongsToMany(db.llm_models, {
      as: 'models_filter',
      foreignKey: {
        name: 'llm_providers_modelsId',
      },
      constraints: false,
      through: 'llm_providersModelsLlm_models',
    });


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










    db.llm_providers.hasMany(db.llm_models, {
      as: 'llm_models_provider',
      foreignKey: {
          name: 'providerId',
      },
      constraints: false,
    });


    db.llm_providers.hasMany(db.llm_api_keys, {
      as: 'llm_api_keys_provider',
      foreignKey: {
          name: 'providerId',
      },
      constraints: false,
    });


    db.llm_providers.hasMany(db.key_rotation_policies, {
      as: 'key_rotation_policies_provider',
      foreignKey: {
          name: 'providerId',
      },
      constraints: false,
    });


















//end loop



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




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

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



  return llm_providers;
};


