const TblMapaFamilia = require("../models/TblMapasFamilias");

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

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

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



const createFamiliaMapa = async (mapaFamiliaData) => {
  return TblMapaFamilia.create(mapaFamiliaData);
};

const updateFamiliaMapa = async (id, mapaFamiliaData) => {
  const mapaFamilia = await TblMapaFamilia.findByPk(id);
  if (!mapaFamilia) {
    throw new Error("MapaFamilia not found");
  }
  return mapaFamilia.update(mapaFamiliaData);
};

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

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

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

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

module.exports = {
  getAllFamiliaMapas,
  getFamiliaMapaById,
  createFamiliaMapa,
  updateFamiliaMapa,
  deleteFamiliaMapa,
};
