import { pool } from "../config/db.js";

/** Master factors for RPN = likelihood × severity (stored in `orac_likelihood_of_harm` / `orac_severity_of_harm`). */

export async function listLikelihood() {
  const [rows] = await pool.query(
    "SELECT `likelihood_of_harm`, `value` FROM `orac_likelihood_of_harm` ORDER BY `value` ASC, `likelihood_of_harm` ASC"
  );
  return rows;
}

export async function listSeverity() {
  const [rows] = await pool.query(
    "SELECT `severity_of_harm`, `value` FROM `orac_severity_of_harm` ORDER BY `value` ASC, `severity_of_harm` ASC"
  );
  return rows;
}

export async function insertLikelihood(likelihoodOfHarm, value) {
  await pool.query(
    "INSERT INTO `orac_likelihood_of_harm` (`likelihood_of_harm`, `value`) VALUES (?, ?)",
    [likelihoodOfHarm, value]
  );
}

export async function updateLikelihood(likelihoodOfHarm, value) {
  const [r] = await pool.query(
    "UPDATE `orac_likelihood_of_harm` SET `value` = ? WHERE `likelihood_of_harm` = ?",
    [value, likelihoodOfHarm]
  );
  return r.affectedRows;
}

export async function deleteLikelihood(likelihoodOfHarm) {
  const [r] = await pool.query(
    "DELETE FROM `orac_likelihood_of_harm` WHERE `likelihood_of_harm` = ?",
    [likelihoodOfHarm]
  );
  return r.affectedRows;
}

export async function insertSeverity(severityOfHarm, value) {
  await pool.query(
    "INSERT INTO `orac_severity_of_harm` (`severity_of_harm`, `value`) VALUES (?, ?)",
    [severityOfHarm, value]
  );
}

export async function updateSeverity(severityOfHarm, value) {
  const [r] = await pool.query(
    "UPDATE `orac_severity_of_harm` SET `value` = ? WHERE `severity_of_harm` = ?",
    [value, severityOfHarm]
  );
  return r.affectedRows;
}

export async function deleteSeverity(severityOfHarm) {
  const [r] = await pool.query(
    "DELETE FROM `orac_severity_of_harm` WHERE `severity_of_harm` = ?",
    [severityOfHarm]
  );
  return r.affectedRows;
}
