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

query_text: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"queued",


"running",


"succeeded",


"failed"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  web_search_queries.associate = (db) => {

    db.web_search_queries.belongsToMany(db.web_search_results, {
      as: 'results',
      foreignKey: {
        name: 'web_search_queries_resultsId',
      },
      constraints: false,
      through: 'web_search_queriesResultsWeb_search_results',
    });

    db.web_search_queries.belongsToMany(db.web_search_results, {
      as: 'results_filter',
      foreignKey: {
        name: 'web_search_queries_resultsId',
      },
      constraints: false,
      through: 'web_search_queriesResultsWeb_search_results',
    });


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

























    db.web_search_queries.hasMany(db.web_search_results, {
      as: 'web_search_results_query',
      foreignKey: {
          name: 'queryId',
      },
      constraints: false,
    });





//end loop



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

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

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




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

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



  return web_search_queries;
};


