const { splitSite } = require("../middlewares");
const db = require("../models");
const Report = db.report;
exports.createReport = (req, res) => {
  // Validate request
  if (!req.body.data) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  // Create a Report
  const report = new Report({
    date: req.body.date || '',
    type: req.body.type || '',
    route_id: req.body.route_id || '',
    driver_name: req.body.driver_name || '',
    route_name: req.body.route_name || '',
    data: req.body.data || [],
    head: req.body.head || [],
    chinese_head: req.body.chinese_head || [],
    checklist_result: req.body.checklist_result || [],
    vehicle_number: req.body.vehicle_number || null,
    site
  });

  // Save Report in the database
  report
    .save(report)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Report."
      });
    });
};
// Retrieve all Reports from the database.
exports.getAllReports = (req, res) => {
  var condition = {};
  condition = splitSite.splitSiteGet(req, condition);
  Report.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving reports."
      });
    });
};
// Retrieve all Active Reports By Date and Type (Admin Reports).
exports.getReportsByDateAndType = (req, res) => {
  const params = req.query;
  const date = params?.date;
  const type = params?.type;
  var condition = { date, type };
  condition = splitSite.splitSiteGet(req, condition);
  Report.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving Reports with Date and Type."
      });
    });
};
// Retrieve reports By RouteId and Type (Senior Route report)
exports.getReportsByRouteIdAndType = (req, res) => {
  const params = req.query;
  const route_id = params?.route_id;
  const type = params?.type;
  var condition = { route_id, type };
  condition = splitSite.splitSiteGet(req, condition);
  Report.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving Reports with Date and Type."
      });
    });
};

// Get One Report by Id
exports.getReport = (req, res) => {

};
// Update a Report by the id in the request
exports.updateReport = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  Report.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update Report with id=${id}. Maybe Report was not found!`
        });
      } else res.send({ success: true, message: "Report was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error updating Report with id=" + id
      });
    });
};

