import Services from '../models/services.js';

// GET /api/services
export const getAll = async (req, res) => {
  try {
    const data = await Services.getAll();
    res.json(data);
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};

// GET /api/services/:id
export const getOne = async (req, res) => {
  try {
    const data = await Services.getById(req.params.id);
    if (!data) return res.status(404).json({ message: 'Service not found.' });
    res.json(data);
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};

// POST /api/services
export const create = async (req, res) => {
  try {
    await Services.create(req.body);
    res.status(201).json({ message: 'Service created.' });
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};

// PUT /api/services/:id
export const update = async (req, res) => {
  try {
    const affected = await Services.update(req.params.id, req.body);
    if (!affected) return res.status(404).json({ message: 'Service not found.' });
    res.json({ message: 'Service updated.' });
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};

// DELETE /api/services/:id
export const remove = async (req, res) => {
  try {
    const affected = await Services.remove(req.params.id);
    if (!affected) return res.status(404).json({ message: 'Service not found.' });
    res.json({ message: 'Service deleted.' });
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
};