const { getDb } = require('../dbPool');

// CREATE
const create = async (type) => {
  const db = await getDb();
  const [result] = await db.query(
    `INSERT INTO IssueTypes (category, subcategory) VALUES (?, ?)`,
    [type.category, type.subcategory]
  );
  return result.insertId;
};

// READ ALL
const getAll = async () => {
  const db = await getDb();
  const [rows] = await db.query(`SELECT issue_type_id, category, subcategory FROM IssueTypes ORDER BY category, subcategory`);
  return rows;
};

// UPDATE
const update = async (id, type) => {
  const db = await getDb();
  const [result] = await db.query(
    `UPDATE IssueTypes SET category = ?, subcategory = ? WHERE id = ?`,
    [type.category, type.subcategory, id]
  );
  return result.affectedRows;
};

// DELETE
const remove = async (id) => {
  const db = await getDb();
  const [result] = await db.query(`DELETE FROM IssueTypes WHERE id = ?`, [id]);
  return result.affectedRows;
};

module.exports = {
  create,
  getAll,
  update,
  remove
};
