import Warehouse from '../models/Warehouse.js';

export const createWarehouse = async (req, res) => {
  try {
    const { warehouseCode, warehouseName, warehouseLocation } = req.body;

    const existingWarehouse = await Warehouse.findOne({ warehouseCode });
    if (existingWarehouse) {
      return res.status(400).json({ message: 'Warehouse code already exists' });
    }

    const warehouse = new Warehouse({
      warehouseCode,
      warehouseName,
      warehouseLocation,
    });

    await warehouse.save();
    res.status(201).json({ message: 'Warehouse created successfully', warehouse });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

export const getAllWarehouses = async (req, res) => {
  try {
    const warehouses = await Warehouse.find().sort({ warehouseName: 1 });
    res.status(200).json(warehouses);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};
