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

sent_at: {
        type: DataTypes.DATE,
      
      

      },

sender: {
        type: DataTypes.ENUM,
      
      

        values: [

"user",


"assistant",


"system"

        ],

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

intent: {
        type: DataTypes.ENUM,
      
      

        values: [

"analyze_symbol",


"compare_symbols",


"news_summary",


"portfolio_question",


"education",


"other"

        ],

      },

risk_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"unknown"

        ],

      },

recommendation: {
        type: DataTypes.ENUM,
      
      

        values: [

"buy",


"hold",


"sell",


"unknown"

        ],

      },

confidence: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  ai_messages.associate = (db) => {

    db.ai_messages.belongsToMany(db.market_symbols, {
      as: 'cited_symbols',
      foreignKey: {
        name: 'ai_messages_cited_symbolsId',
      },
      constraints: false,
      through: 'ai_messagesCited_symbolsMarket_symbols',
    });

    db.ai_messages.belongsToMany(db.market_symbols, {
      as: 'cited_symbols_filter',
      foreignKey: {
        name: 'ai_messages_cited_symbolsId',
      },
      constraints: false,
      through: 'ai_messagesCited_symbolsMarket_symbols',
    });


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





































//end loop



    db.ai_messages.belongsTo(db.ai_chat_sessions, {
      as: 'ai_chat_session',
      foreignKey: {
        name: 'ai_chat_sessionId',
      },
      constraints: false,
    });




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

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



  return ai_messages;
};


