const TblMaquina = require("../models/TblMaquinas");

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

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

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



const createMaquina = async (maquinaData) => {
  return TblMaquina.create(maquinaData);
};

const updateMaquina = async (id, maquinaData) => {
  const maquina = await TblMaquina.findByPk(id);
  if (!maquina) {
    throw new Error("Maquina not found");
  }
  return maquina.update(maquinaData);
};

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

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

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

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

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