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

author_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"user",


"assistant",


"system"

        ],

      },

content: {
        type: DataTypes.TEXT,
      
      

      },

sent_at: {
        type: DataTypes.DATE,
      
      

      },

message_kind: {
        type: DataTypes.ENUM,
      
      

        values: [

"question",


"answer",


"tool_result",


"note"

        ],

      },

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

  chat_messages.associate = (db) => {

    db.chat_messages.belongsToMany(db.message_citations, {
      as: 'citations',
      foreignKey: {
        name: 'chat_messages_citationsId',
      },
      constraints: false,
      through: 'chat_messagesCitationsMessage_citations',
    });

    db.chat_messages.belongsToMany(db.message_citations, {
      as: 'citations_filter',
      foreignKey: {
        name: 'chat_messages_citationsId',
      },
      constraints: false,
      through: 'chat_messagesCitationsMessage_citations',
    });


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























    db.chat_messages.hasMany(db.message_citations, {
      as: 'message_citations_message',
      foreignKey: {
          name: 'messageId',
      },
      constraints: false,
    });





//end loop



    db.chat_messages.belongsTo(db.chat_threads, {
      as: 'thread',
      foreignKey: {
        name: 'threadId',
      },
      constraints: false,
    });

    db.chat_messages.belongsTo(db.users, {
      as: 'author_user',
      foreignKey: {
        name: 'author_userId',
      },
      constraints: false,
    });

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




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

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



  return chat_messages;
};


