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

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

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

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


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

const updateMaquina = async (req, res) => {
  const { id } = req.params;
  const { body } = req;
  try {
    const updatedMaquina = await maquinaService.updateMaquina(id, body);
    if (!updatedMaquina) {
      return res.status(404).json({ error: "Maquina not found" });
    }
    res.json(updatedMaquina);
  } catch (error) {     
    logger.error(`Error updating maquina: ${error.message}`);
    res.status(500).json({ error: "Internal Server Error" });
  }
};

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

module.exports = {
  getAllMaquinas,
  getMaquinaById,
  createMaquina,
  updateMaquina,
  deleteMaquina,
};
