const TblArtigoMaquina = require("../models/TblArtigosMaquina");

const getAllArtigosMaquina = async (queryParams = {}) => {
  return TblArtigoMaquina.findAll({
    where: queryParams,
  });
};

const getArtigoMaquinaById = async (id, queryParams = {}) => {
  const whereCondition = { intId: id, ...queryParams };

  return TblArtigoMaquina.findOne({
    where: whereCondition,
  });
};





const createArtigoMaquina = async (artigoMaquinaData) => {
  return TblArtigoMaquina.create(artigoMaquinaData);
};

const updateArtigoMaquina = async (id, artigoMaquinaData) => {
  const artigoMaquina = await TblArtigoMaquina.findByPk(id);
  if (!artigoMaquina) {
    throw new Error("ArtigoMaquina not found");
  }
  return artigoMaquina.update(artigoMaquinaData);
};

const deleteArtigoMaquina = async (id) => {
  // Assuming TblArtigoMaquina is your Sequelize model
  const result = await TblArtigoMaquina.update(
    { bitEliminado: true },
    { where: { intId: id } }
  );

  // The 'result' object contains information about the operation
  const affectedRows = result[0];

  if (affectedRows === 0) {
    throw new Error("ArtigoMaquina not found");
  }

  // You might want to return additional information if needed
  return { success: true, message: "ArtigoMaquina soft deleted successfully" };
};

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