const { splitSite } = require("../middlewares");
const db = require("../models");
const Vehicle = db.vehicle;
exports.createVehicle = (req, res) => {
  // Validate request
  if (!req.body.tag) {
    res.status(400).send({ message: "Content can not be empty!" });
    return;
  }
  const site = splitSite.findSiteNumber(req);
  // Create a Vehicle
  const vehicle = new Vehicle({
    vehicle_number: req.body.vehicle_number,
    tag: req.body.tag || '',
    ezpass: req.body.ezpass || '',
    gps_tag: req.body.gps_tag || '',
    mileage: req.body.mileage || 0,
    capacity: req.body.capacity || 0,
    make: req.body.make || '',
    vehicle_model: req.body.vehicle_model || '',
    year: req.body.year || '',
    checklist: req.body.checklist || '',
    status: 'active',
    site
  });

  // Save Vehicle in the database
  vehicle
    .save(vehicle)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Vehicle."
      });
    });
};
// Retrieve all Vehicles from the database.
exports.getAllVehicles = (req, res) => {
  var condition = {};
  condition = splitSite.splitSiteGet(req, condition);
  Vehicle.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving Vehicles."
      });
    });
};
// Retrieve all Active Vehicles from the database.
exports.getAllActiveVehicles = (req, res) => {
  var condition = { status: 'active' };
  condition = splitSite.splitSiteGet(req, condition);
  Vehicle.find(condition)
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving Vehicles."
      });
    });
};
// Get One Vehicle by Id
exports.getVehicle = (req, res) => {
  const id = req.params.id;
  Vehicle.findById(id)
    .then(data => {
      if (!data)
        res.status(404).send({ message: "Not found Vehicle with id " + id });
      else res.send(data);
    })
    .catch(err => {
      res
        .status(500)
        .send({ message: "Error retrieving Vehicle with id=" + id });
    });
};
// Update a Vehicle by the id in the request
exports.updateVehicle = (req, res) => {
  if (!req.body) {
    return res.status(400).send({
      message: "Data to update can not be empty!"
    });
  }
  const id = req.params.id;
  Vehicle.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot update Vehicle with id=${id}. Maybe Vehicle was not found!`
        });
      } else res.send({ success: true, message: "Vehicle was updated successfully." });
    })
    .catch(err => {
      res.status(500).send({
        success: false,
        message: "Error" + (err.message || "") + "updating Vehicle with id=" + id
      });
    });
};
// Soft Delete a Vehicle
exports.deactivateVehicle = (req, res) => {

};

// Delete a Vehicle by id
exports.deleteVehicle= (req, res) => {
  const id = req.params.id;
  Vehicle.findByIdAndRemove(id)
    .then(data => {
      if (!data) {
        res.status(404).send({
          message: `Cannot delete Vehicle with id=${id}. Maybe Vehicle was not found!`
        });
      } else {
        res.send({
          message: "Vehicle was deleted successfully!"
        });
      }
    })
    .catch(err => {
      res.status(500).send({
        message: "Could not delete Vehicle with id=" + id + ": " + err.message || ""
      });
    });
};
