#' Differential (delta) AUC between two screening conditions
#'
#' Contrasts drug potency between two experimental conditions by reshaping a
#' long per-drug/per-condition AUC table to one row per drug and computing the
#' differential AUC together with its within-experiment z-score. This is the
#' core scoring step for two-condition screens (for example irradiated versus
#' non-irradiated, or treated versus control).
#'
#' @param data A long-format data frame with one row per drug and condition.
#' @param drug_col Name of the drug identifier column, as a string. Default
#'   `"drug_name"`.
#' @param condition_col Name of the condition column, as a string. Default
#'   `"condition"`. The column must contain exactly the two values given in
#'   `conditions`.
#' @param auc_col Name of the AUC column, as a string. Default `"auc"`.
#' @param conditions Character vector of length two giving the baseline and
#'   treatment condition, in the order `c(baseline, treatment)`. The delta is
#'   computed as `treatment - baseline`. Defaults to the two levels found in
#'   `condition_col`.
#'
#' @return A data frame with one row per drug containing the two per-condition
#'   AUC columns (named `auc_<condition>`), `delta_auc` and `zscore_delta_auc`
#'   (the z-scored `delta_auc` across all drugs).
#'
#' @details A negative `delta_auc` indicates the treatment condition sensitises
#'   cells to the drug relative to baseline (lower AUC = more killing). The
#'   z-score standardises `delta_auc` across all drugs within the experiment so
#'   that hits can be selected on a common scale (see [select_hits_sigma()]).
#'
#' @examples
#' auc_long <- data.frame(
#'   drug_name = rep(c("DrugA", "DrugB", "DrugC"), each = 2),
#'   condition = rep(c("Gy0", "Gy2"), times = 3),
#'   auc = c(5.0, 3.1, 4.2, 4.0, 6.0, 2.0)
#' )
#' compute_delta_auc(auc_long, conditions = c("Gy0", "Gy2"))
#'
#' @seealso [select_hits_cutoff()], [select_hits_sigma()]
#' @export
compute_delta_auc <- function(data,
                              drug_col = "drug_name",
                              condition_col = "condition",
                              auc_col = "auc",
                              conditions = NULL) {
  for (col in c(drug_col, condition_col, auc_col)) {
    if (!col %in% names(data)) {
      stop("Column '", col, "' not found in `data`.", call. = FALSE)
    }
  }

  if (is.null(conditions)) {
    conditions <- unique(as.character(data[[condition_col]]))
  }
  if (length(conditions) != 2) {
    stop(
      "`conditions` must identify exactly two conditions; found ",
      length(conditions), ".",
      call. = FALSE
    )
  }
  baseline <- conditions[1]
  treatment <- conditions[2]

  long <- data[as.character(data[[condition_col]]) %in% conditions,
    c(drug_col, condition_col, auc_col),
    drop = FALSE
  ]
  names(long) <- c("drug_name", "condition", "auc")
  long$condition <- as.character(long$condition)

  wide <- tidyr::pivot_wider(
    unique(long),
    names_from = "condition",
    values_from = "auc",
    names_prefix = "auc_"
  )

  base_col <- paste0("auc_", baseline)
  treat_col <- paste0("auc_", treatment)

  wide$delta_auc <- wide[[treat_col]] - wide[[base_col]]
  wide$zscore_delta_auc <- as.numeric(scale(wide$delta_auc))

  as.data.frame(wide)
}
