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

query_text: {
        type: DataTypes.TEXT,
      
      

      },

jurisdiction: {
        type: DataTypes.TEXT,
      
      

      },

source_scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"federal",


"state",


"both"

        ],

      },

searched_at: {
        type: DataTypes.DATE,
      
      

      },

filters_json: {
        type: DataTypes.TEXT,
      
      

      },

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

  research_queries.associate = (db) => {


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




















    db.research_queries.hasMany(db.research_results, {
      as: 'research_results_research_query',
      foreignKey: {
          name: 'research_queryId',
      },
      constraints: false,
    });












//end loop



    db.research_queries.belongsTo(db.users, {
      as: 'requested_by',
      foreignKey: {
        name: 'requested_byId',
      },
      constraints: false,
    });

    db.research_queries.belongsTo(db.matters, {
      as: 'matter',
      foreignKey: {
        name: 'matterId',
      },
      constraints: false,
    });




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

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



  return research_queries;
};


