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

thread_title: {
        type: DataTypes.TEXT,
      
      

      },

thread_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"advice",


"analysis",


"data_question",


"alert_followup"

        ],

      },

last_message_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  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.chat_threads.hasMany(db.chat_messages, {
      as: 'chat_messages_thread',
      foreignKey: {
          name: 'threadId',
      },
      constraints: false,
    });






//end loop



    db.chat_threads.belongsTo(db.workspaces, {
      as: 'workspace',
      foreignKey: {
        name: 'workspaceId',
      },
      constraints: false,
    });

    db.chat_threads.belongsTo(db.users, {
      as: 'started_by',
      foreignKey: {
        name: 'started_byId',
      },
      constraints: false,
    });

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




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

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



  return chat_threads;
};


