// Requiring our models and passport as we've configured it
let db = require("../models");
let passport = require("../config/passport");
const { Op } = require("sequelize");
var dataQuery;

module.exports = function (app) {

  //retriving sequelize model test
  app.get("/api/omgTest/:cat/:price1/:price2", function (req, res) {

    db.Omg.findAll({
      where: {
        Category: {
          [Op.like]: req.params.cat+'%',
        },
        SalePrice: {
          [Op.between]: [req.params.price1, req.params.price2],
        }
      }
    }).then(function (data) {
      dataQuery = data;
      res.json(data)
      
    })

  });

  app.get('/output', function(req, res){
    for(var i =0; i < 1; i++){
    res.render('output', {
      ProductUrl: dataQuery[i].ProductUrl,
      ProductName: dataQuery[i].ProductName,
      Description: dataQuery[i].Description,
      SalePrice: dataQuery[i].SalePrice
     })
  }
  })
  // Using the passport.authenticate middleware with our local strategy.
  // If the user has valid login credentials, send them to the members page.
  // Otherwise the user will be sent an error
  app.post("/api/login", passport.authenticate("local"), function (req, res) {
    res.json(req.user);
  });

  // Route for signing up a user. The user's password is automatically hashed and stored securely thanks to
  // how we configured our Sequelize User Model. If the user is created successfully, proceed to log the user in,
  // otherwise send back an error
  app.post("/api/signup", function (req, res) {
    db.User.create({
      email: req.body.email,
      password: req.body.password
    })
      .then(function () {
        res.redirect(307, "/api/login");
      })
      .catch(function (err) {
        res.status(401).json(err);
      });
  });

  // Route for logging user out
  app.get("/logout", function (req, res) {
    req.logout();
    res.redirect("/");
  });

  // Route for getting some data about our user to be used client side
  app.get("/api/user_data", function (req, res) {
    if (!req.user) {
      // The user is not logged in, send back an empty object
      res.json({});
    } else {
      // Otherwise send back the user's email and id
      // Sending back a password, even a hashed password, isn't a good idea
      res.json({
        email: req.user.email,
        id: req.user.id
      });
    }
  });
};
