const Department = require('../models/Department');

exports.addDepartment = async (req, res) => {
  try {
    const { departmentCode, departmentName } = req.body;

    if (!departmentCode || !departmentName) {
      return res.status(400).json({ message: 'Department code and name are required' });
    }

    const existing = await Department.findOne({ departmentCode: departmentCode.trim() });
    if (existing) {
      return res.status(409).json({ message: 'Department code already exists' });
    }

    const dept = await Department.create({ departmentCode, departmentName });
    res.status(201).json(dept);
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};

exports.getDepartments = async (req, res) => {
  try {
    const departments = await Department.find();
    res.json(departments);
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};
