

const SpecialNeed = require('../models/SpecialNeed'); // Assuming you have a Mongoose model

// Create a new document
exports.createSpecialNeed = async (req, res) => {
  try {
    const specialNeed = new SpecialNeed(req.body);
    const result = await specialNeed.save();
    res.status(201).json(result);
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
};

// Get all documents
exports.getAllSpecialNeeds = async (req, res) => {
  try {
  
    const specialNeeds = await SpecialNeed.find().populate('categoryId');
   // console.log(specialNeeds)
    res.status(200).json(specialNeeds);
  } catch (error) {
    console.log(error.message)
    res.status(500).json({ error: error.message });
  }
};

// Get a single document by ID
exports.getSpecialNeedById = async (req, res) => {
  try {
    const specialNeed = await SpecialNeed.findById(req.params.id);
    if (!specialNeed) {
      return res.status(404).json({ message: 'Special Need not found' });
    }
    res.status(200).json(specialNeed);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

// Update a document by ID
exports.updateSpecialNeed = async (req, res) => {
  try {
    const specialNeed = await SpecialNeed.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true });
    if (!specialNeed) {
      return res.status(404).json({ message: 'Special Need not found' });
    }
    res.status(200).json(specialNeed);
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
};

// Delete a document by ID
exports.deleteSpecialNeed = async (req, res) => {
  try {
    const specialNeed = await SpecialNeed.findByIdAndDelete(req.params.id);
    if (!specialNeed) {
      return res.status(404).json({ message: 'Special Need not found' });
    }
    res.status(200).json({ message: 'Special Need deleted successfully' });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};
