import { postgresConnection as cn } from '../../config/helpers.js';
import { getMultipleRecipes } from '../recipes/recipeController.js';
const pgp = require('pg-promise')();
const db = pgp(cn);

module.exports = {
  searchRecipes: (request, response, next) => {
    const queryObj = {
      name: 'search-recipes',
      text: 'SELECT id FROM recipes WHERE title ~* 'vegan';',
      // values: [request.body.title, request.body.prep_steps, request.body.cook_steps],
    };

    console.log('REQUEST.BODY****************',request.body)
    db.one(queryObj)
      .then((data) => {
        response.status(200);
        response.json(data);
      })
      .catch((error) => {
        response.status(500);
        response.json({
          error: `Error code: ${error.code}, Error message: ${error.detail}`,
        });
      });
  },

  // searchRecipes: (request, response, next) => {
  //   // munge space-separated words into a single OR-separated string
  //   // NOTE: this *may* be breaking query sanitization provided by pg-promise
  //   if (!request.body) {
  //     request.body = {};
  //   } else {
  //     request.body._queryResultIds = [];
  //   }
  //
  //   const query = decodeURIComponent(request.params.q)
  //     .split(' ')
  //     .join(' | ');
  //
  //   const queryObj = {
  //     name: 'search-recipes-table',
  //     text: `SELECT
  //              *
  //            FROM (
  //              SELECT
  //                to_tsvector(CAST(recipes.id as VARCHAR)) || ' ' ||
  //                to_tsvector(CAST(recipes.ingredients as VARCHAR)) || ' ' ||
  //                to_tsvector(CAST(recipes.cook_steps as VARCHAR)) || ' ' ||
  //                to_tsvector(CAST(recipes.prep_steps as VARCHAR)) || ' ' ||
  //                to_tsvector(CAST(recipes.finish_steps as VARCHAR)) || ' ' ||
  //                to_tsvector(coalesce((string_agg(recipes.title, ' ')), ''))
  //                  as document
  //              FROM
  //                recipes
  //              GROUP BY
  //                recipes.id
  //            ) p_search
  //            WHERE
  //              p_search.document @@ to_tsquery($1);`,
  //     values: [query],
  //   };
  //
  //   db.query(queryObj)
  //     .then((data) => {
  //       response.status(201);
  //       request.body.recipes = data.map((val) => {
  //         return parseInt(val.document.match(/(?:'1':)(\d+)(?=[,\s])/)[1], 10);
  //       });
  //       // console.log(request.body._queryResultIds);
  //
  //       return getMultipleRecipes(request, response, next);
  //     })
  //     .catch((error) => {
  //       // console.log('query error', error)
  //       response.json(error);
  //       next();
  //     });
  // },
};

@@ -207,16 +207,44 @@
 rts = %7B%0A
+  // const vegan = 'vegan';%0A
   search
@@ -332,17 +332,22 @@
 h-recipe
-s
+-title
 ',%0A     
@@ -392,24 +392,23 @@
 itle ~* 
-'
+%22
 vegan
-';
+%22
 ',%0A     
@@ -424,76 +424,13 @@
 s: %5B
-request.body.title, request.body.prep_steps, request.body.cook_steps
+vegan
 %5D,%0A 
@@ -435,24 +435,27 @@
 %0A    %7D;%0A%0A   
+ //
  console.log

fix syntax error that was breaking npm dev-start