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

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

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

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


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

const updateJustificacao = async (req, res) => {
  const { id } = req.params;
  const { body } = req;
  try {
    const updatedJustificacao = await justificacaoService.updateJustificacao(id, body);
    if (!updatedJustificacao) {
      return res.status(404).json({ error: "Justificacao not found" });
    }
    res.json(updatedJustificacao);
  } catch (error) {     
    logger.error(`Error updating justificacao: ${error.message}`);
    res.status(500).json({ error: "Internal Server Error" });
  }
};

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

module.exports = {
  getAllJustificacoes,
  getJustificacaoById,
  createJustificacao,
  updateJustificacao,
  deleteJustificacao,
};
