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

thread_title: {
        type: DataTypes.TEXT,
      
      

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  ai_chat_threads.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_chat_threads.hasMany(db.ai_chat_messages, {
      as: 'ai_chat_messages_thread',
      foreignKey: {
          name: 'threadId',
      },
      constraints: false,
    });































































//end loop



    db.ai_chat_threads.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });




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

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



  return ai_chat_threads;
};


