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

// RPN Likelihood functions
const getAllRpnLikelihood = async () => {
  const [rows] = await pool.query('SELECT * FROM rpn_likelihood_of_harm');
  return rows;
};

const createRpnLikelihood = async (likelihood_of_harm, value) => {
  await pool.query('INSERT INTO rpn_likelihood_of_harm (likelihood_of_harm, value) VALUES (?, ?)', [likelihood_of_harm, value]);
};

const updateRpnLikelihood = async (likelihood_of_harm, value) => {
  const [result] = await pool.query('UPDATE rpn_likelihood_of_harm SET value = ? WHERE likelihood_of_harm = ?', [value, likelihood_of_harm]);
  return result.affectedRows > 0;
};

const deleteRpnLikelihood = async (likelihood_of_harm) => {
  const [result] = await pool.query('DELETE FROM rpn_likelihood_of_harm WHERE likelihood_of_harm = ?', [likelihood_of_harm]);
  return result.affectedRows > 0;
};

// RPN Severity functions
const getAllRpnSeverity = async () => {
  const [rows] = await pool.query('SELECT * FROM rpn_severity_of_harm');
  return rows;
};

const createRpnSeverity = async (severity_of_harm, value) => {
  await pool.query('INSERT INTO rpn_severity_of_harm (severity_of_harm, value) VALUES (?, ?)', [severity_of_harm, value]);
};

const updateRpnSeverity = async (severity_of_harm, value) => {
  const [result] = await pool.query('UPDATE rpn_severity_of_harm SET value = ? WHERE severity_of_harm = ?', [value, severity_of_harm]);
  return result.affectedRows > 0;
};

const deleteRpnSeverity = async (severity_of_harm) => {
  const [result] = await pool.query('DELETE FROM rpn_severity_of_harm WHERE severity_of_harm = ?', [severity_of_harm]);
  return result.affectedRows > 0;
};

module.exports = {
  getAllRpnLikelihood,
  createRpnLikelihood,
  updateRpnLikelihood,
  deleteRpnLikelihood,
  getAllRpnSeverity,
  createRpnSeverity,
  updateRpnSeverity,
  deleteRpnSeverity
};

