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

query_text: {
        type: DataTypes.TEXT,
      
      

      },

mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"web_search",


"database_search",


"hybrid"

        ],

      },

sensitivity: {
        type: DataTypes.ENUM,
      
      

        values: [

"general",


"professional",


"high_stakes"

        ],

      },

queried_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  facta_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.facta_queries.hasMany(db.facta_answers, {
      as: 'facta_answers_facta_query',
      foreignKey: {
          name: 'facta_queryId',
      },
      constraints: false,
    });






//end loop



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

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




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

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



  return facta_queries;
};


