var express = require("express");
var router = express.Router();
var models = require("express-cassandra");
const db = require("../services/db");
const moment = require("moment");
/* GET users listing. */
router.get("/", function (req, res, next) {
  res.send("respond with a resource");
});

// router.get("/person/:name/:surname/:age", function (req, res) {
//   // res.send(
//   //   "name: " +
//   //     req.params.name +
//   //     ", surname:" +
//   //     req.params.surname +
//   //     ", age:" +
//   //     req.params.age
//   // );
//   // var person = new models.instance.Person({
//   //   name: req.params.name,
//   //   surname: req.params.surname,
//   //   age: parseInt(req.params.age),
//   //   created: Date.now(),
//   // });
//   // person.save(function (err) {
//   //   if (err) {
//   //     console.log(err);
//   //     return;
//   //   }
//   //   console.log("person saved!");
//   // });
//   models.instance.Person.findOne({ name: "John" }, function (err, person) {
//     if (err) throw err;
//     console.log("Found:", person);
//     res.send(person);
//   });
// });

router.get("/getPerson/:name/:surname/:age", async function (req, res) {
  res.send(
    "name: " +
      req.params.name +
      ", surname:" +
      req.params.surname +
      ", age:" +
      req.params.age
  );
  var person = new models.instance.ChatHeaders({
    chatName: req.params.name,
    lastMsg: req.params.surname,
    userId: 999,
    userId2: 888,
  });
  person.save(function (err) {
    if (err) {
      console.log(err);
      return;
    }
    console.log("person saved!");
  });
  // var message = new models.instance.Message({
  //   msg: "data.msg",
  //   roomId: 0001,
  //   userId: 0002,
  //   chatName: "activity.name",
  //   type: "data.type",
  // });
  // message = await message.saveAsync();
  // console.log("message:", message);
  // res.send(message);

  // models.instance.Person.findOne({ name: "John" }, function (err, person) {
  //   if (err) throw err;
  //   console.log("Found:", person);
  //   res.send(person);
  // });
});

router.get("/person", async function (req, res) {
  try {
    let data = {
      msg: "HeYYY",
    };
    let socket = {
      roomId: 140,
      userId: 109,
    };
    let currentTime = moment().format("YYYY-MM-DD HH:mm:ss");

    var x = `update chat_headers set lastMsg = '${data.msg}', updatedAt = '${currentTime}' where
      userId = '${socket.userId}' and roomId = '${socket.roomId}'`;
    let qyy = await db.query(x);
    console.log("QYYYY:", qyy);
    return res.send(qyy);
    var query = {
      userId: 101,
      // also allowed $asc and $desc and complex order like $orderby: {'$asc' : ['k1','k2'] }
      // $orderby: { $desc: "userId" },
      //limit the result set to 10 rows, $per_partition_limit is also supported
      $limit: 10,
    };
    // models.instance.ChatHeaders.find(
    //   {},
    //   { raw: true, allow_filtering: true },
    //   function (err, person) {
    //     if (err) console.log("ERR:", err);
    //     console.log("Found:", person);
    //     res.send(person);
    //   }
    // );

    // models.instance.Message.find(
    //   { roomId: 140 },
    //   { raw: true, allow_filtering: true },
    //   function (err, person) {
    //     if (err) console.log("ERR:", err);
    //     console.log("Found:", person);
    //     res.send(person);
    //   }
    // );
  } catch (error) {
    console.log("ERR:", error);
  }
});

module.exports = router;
