const artigoMaquinaService = require("../services/artigoMaquinaService");
const logger = require("../utils/logger");

// Controller methods for artigoMaquina operations
const getAllArtigosMaquina = async (req, res) => {
  const queryParams = req.query; // Assuming you want to get parameters from the request body

  try {
    const artigosMaquina = await artigoMaquinaService.getAllArtigosMaquina(queryParams);
    res.json(artigosMaquina);
  } catch (error) {
    logger.error(`Error getting all artigosMaquina: ${error.message}`);
    res.status(500).json({ error: "Internal Server Error" });
  }
};

const getArtigoMaquinaById = async (req, res) => {
  const { id } = req.params;
  const queryParams = req.body; // Assuming you want to get parameters from the request body
  try {
    const artigoMaquina = await artigoMaquinaService.getArtigoMaquinaById(id, queryParams);
    if (!artigoMaquina) {
      return res.status(404).json({ error: "ArtigoMaquina not found" });
    }
    res.json(artigoMaquina);
  } catch (error) {
    logger.error(`Error getting artigoMaquina by ID: ${error.message}`);
    res.status(500).json({ error: "Internal Server Error" });
  }
};


const createArtigoMaquina = async (req, res) => {
  const { body } = req;
  try {
    const newArtigoMaquina = await artigoMaquinaService.createArtigoMaquina(body);
    res.status(201).json(newArtigoMaquina);
  } catch (error) {
    logger.error(`Error creating artigoMaquina: ${error.message}`);
    res.status(500).json({ error: "Internal Server Error" });
  }
};

const updateArtigoMaquina = async (req, res) => {
  const { id } = req.params;
  const { body } = req;
  try {
    const updatedArtigoMaquina = await artigoMaquinaService.updateArtigoMaquina(id, body);
    if (!updatedArtigoMaquina) {
      return res.status(404).json({ error: "ArtigoMaquina not found" });
    }
    res.json(updatedArtigoMaquina);
  } catch (error) {     
    logger.error(`Error updating artigoMaquina: ${error.message}`);
    res.status(500).json({ error: "Internal Server Error" });
  }
};

const deleteArtigoMaquina = async (req, res) => {
  const { id } = req.params;
  try {
    const deletedArtigoMaquina = await artigoMaquinaService.deleteArtigoMaquina(id);
    if (!deletedArtigoMaquina) {
      return res.status(404).json({ error: "ArtigoMaquina not found" });
    }
    res.json({ message: "ArtigoMaquina deleted successfully" });
  } catch (error) {
    logger.error(`Error deleting artigoMaquina: ${error.message}`);
    res.status(500).json({ error: "Internal Server Error" });
  }
};

module.exports = {
  getAllArtigosMaquina,
  getArtigoMaquinaById,
  createArtigoMaquina,
  updateArtigoMaquina,
  deleteArtigoMaquina,
};
