const Warehouse = require("../models/Warehouse");

const createWarehouse = async (req, res) => {
  try {
    const existingWarehouse = await Warehouse.findOne({ warehouseCode: req.body.warehouseCode });
    if (existingWarehouse) {
      return res.status(400).json({ message: "Warehouse code already exists" });
    }

    const warehouse = await Warehouse.create(req.body);
    res.status(201).json({ message: "Warehouse recorded successfully", warehouse });
  } catch (error) {
    res.status(400).json({ message: "Warehouse was not recorded", error: error.message });
  }
};

const getWarehouses = async (req, res) => {
  try {
    const warehouses = await Warehouse.find().sort({ createdAt: -1 });
    res.json(warehouses);
  } catch (error) {
    res.status(500).json({ message: "Warehouses could not be retrieved", error: error.message });
  }
};

module.exports = {
  createWarehouse,
  getWarehouses
};
