/**
 * Shipment controller
 */

const {
  createShipment,
  getAllShipments,
  getShipmentByNumber,
  updateShipment,
  deleteShipment
} = require('../models/shipment.model');

async function create(req, res) {
  const shipment = await createShipment(req.body);
  return res.status(201).json(shipment);
}

async function getAll(_req, res) {
  const shipments = await getAllShipments();
  return res.json({ data: shipments });
}

async function getOne(req, res) {
  const shipment = await getShipmentByNumber(req.params.shipmentNumber);
  if (!shipment) return res.status(404).json({ error: 'NOT_FOUND', message: 'Shipment not found' });
  return res.json(shipment);
}

async function update(req, res) {
  const updated = await updateShipment({ shipmentNumber: req.params.shipmentNumber, ...req.body });
  if (!updated) return res.status(404).json({ error: 'NOT_FOUND', message: 'Shipment not found' });
  return res.json(updated);
}

async function deleteOne(req, res) {
  const ok = await deleteShipment(req.params.shipmentNumber);
  if (!ok) return res.status(404).json({ error: 'NOT_FOUND', message: 'Shipment not found' });
  return res.status(204).send();
}

module.exports = { create, getAll, getOne, update, deleteOne };

