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

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

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

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

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

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

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

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

module.exports = getAndFilterHMOs;
