const pool = require("../config/db");

/**
 * Get combined-question relationships for a set of question IDs.
 * Returns rows where both main_question_id and child_question_id are in the given set
 * (so only relationships relevant to this form are returned).
 * @param {number[]} questionIds
 * @returns {Promise<Array<{ main_question_id: number, child_question_id: number }>>}
 */
const getCombinedQuestionsByQuestionIds = async (questionIds) => {
  if (!questionIds || questionIds.length === 0) return [];
  const placeholders = questionIds.map(() => "?").join(",");
  const [rows] = await pool.query(
    `SELECT main_question_id, child_question_id
     FROM sat_combined_question
     WHERE main_question_id IN (${placeholders})
       AND child_question_id IN (${placeholders})`,
    [...questionIds, ...questionIds]
  );
  return rows || [];
};

module.exports = {
  getCombinedQuestionsByQuestionIds,
};
