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

name: {
        type: DataTypes.TEXT,
      
      

      },

handle: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

system_prompt: {
        type: DataTypes.TEXT,
      
      

      },

persona_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"general",


"coder",


"debugger",


"reviewer",


"planner",


"designer",


"devops",


"qa",


"security"

        ],

      },

priority_rank: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  ai_agents.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_agents.hasMany(db.agent_skill_assignments, {
      as: 'agent_skill_assignments_agent',
      foreignKey: {
          name: 'agentId',
      },
      constraints: false,
    });



    db.ai_agents.hasMany(db.messages, {
      as: 'messages_sender_agent',
      foreignKey: {
          name: 'sender_agentId',
      },
      constraints: false,
    });





    db.ai_agents.hasMany(db.debug_sessions, {
      as: 'debug_sessions_assigned_agent',
      foreignKey: {
          name: 'assigned_agentId',
      },
      constraints: false,
    });


    db.ai_agents.hasMany(db.debug_fixes, {
      as: 'debug_fixes_proposed_by',
      foreignKey: {
          name: 'proposed_byId',
      },
      constraints: false,
    });










//end loop



    db.ai_agents.belongsTo(db.ai_models, {
      as: 'default_model',
      foreignKey: {
        name: 'default_modelId',
      },
      constraints: false,
    });

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



    db.ai_agents.hasMany(db.file, {
      as: 'avatar_images',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.ai_agents.getTableName(),
        belongsToColumn: 'avatar_images',
      },
    });


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

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



  return ai_agents;
};


