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

anchor_key: {
        type: DataTypes.TEXT,
      
      

      },

anchor_excerpt: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

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

  comment_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.comment_threads.hasMany(db.comments, {
      as: 'comments_thread',
      foreignKey: {
          name: 'threadId',
      },
      constraints: false,
    });















//end loop



    db.comment_threads.belongsTo(db.documents, {
      as: 'document',
      foreignKey: {
        name: 'documentId',
      },
      constraints: false,
    });

    db.comment_threads.belongsTo(db.user_stories, {
      as: 'story',
      foreignKey: {
        name: 'storyId',
      },
      constraints: false,
    });

    db.comment_threads.belongsTo(db.users, {
      as: 'created_by_user',
      foreignKey: {
        name: 'created_by_userId',
      },
      constraints: false,
    });

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




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

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



  return comment_threads;
};


