const db = require("../../../managers/dbConfig");

const getFamiliesAndGroupData = async (req, res) => {
  const { item_name } = req.pagination; // Get 'name' filter from client if provided

  let sql = `SELECT * FROM families`;
  const values = [];

  if (item_name) {
    sql += ` WHERE name LIKE ?`; // Allow filtering by families name
    values.push(`%${item_name}%`);
  }

  sql += ` ORDER BY name ASC`; // Always sort alphabetically

  const [rows] = await db.execute(sql, values);

  if (rows.length === 0) throw "No match found";

  res.status(200).json({ status: "success", data: rows });
};

module.exports = getFamiliesAndGroupData;
