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({
    // Basic Information
    vehicle_number: req.body.vehicle_number,
    responsible_driver: req.body.responsible_driver || '',
    responsible_driver_id: req.body.responsible_driver_id || '',
    capacity: req.body.capacity || 0,
    mileage: req.body.mileage || 0,
    make: req.body.make || '',
    vehicle_model: req.body.vehicle_model || '',
    year: req.body.year || '',
    vin: req.body.vin || '',
    tag: req.body.tag || '',
    gps_tag: req.body.gps_tag || '',
    ezpass: req.body.ezpass || '',
    has_lift_equip: req.body.has_lift_equip,
    fuel_type: req.body.fuel_type || '',
    title: req.body.title || '',
    title_other: req.body.title_other || '',
    
    // Compliance & Deadlines
    insurance_start_date: req.body.insurance_start_date || '',
    vehicle_registration_date: req.body.vehicle_registration_date || '',
    
    // Legacy fields (keeping for backward compatibility)
    insurance_expire_on: req.body.insurance_expire_on || '',
    title_registration_on: req.body.title_registration_on || '',
    emission_test_on: req.body.emission_test_on || '',
    oil_change_mileage: req.body.oil_change_mileage,
    oil_change_date: req.body.oil_change_date || '',
    
    // Check List
    checklist: req.body.checklist || [],
    
    // Additional Information
    note: req.body.note || '',
    
    // System fields
    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 || ""
      });
    });
};
