#' Compose footnotes for univariate strata
#' @keywords internal
.footnotes_uni_strata <- function(approach) {
  c(.abbrev_note(approach))
}

#' Describe the stratification variable once without repeating it in headings
#' @keywords internal
#' @noRd
.stratified_by_note <- function(stratifier) {
  paste0("Stratified by: ", stratifier, ".")
}

#' Compose footnotes for multivariable strata (abbrev + per-stratum N)
#' @keywords internal
.footnotes_multi_strata <- function(approach, fits_by_stratum, stratifier) {
  n_lines <- vapply(names(fits_by_stratum), function(k) {
    n_used <- tryCatch(stats::nobs(fits_by_stratum[[k]]), error = function(e) NA_integer_)
    if (!is.na(n_used)) {
      paste0(stratifier, " = ", k, ": N = ", n_used,
             " complete observations included in the multivariable model")
    } else {
      paste0(stratifier, " = ", k, ": complete observations included in the multivariable model")
    }
  }, character(1))
  c(.abbrev_note(approach), n_lines)
}
#' @keywords internal
#' @noRd
.ref_note <- function() {
  "Ref. = reference category."
}

#' @keywords internal
#' @noRd
.validate_show_ref <- function(show_ref) {
  if (!is.logical(show_ref) || length(show_ref) != 1L || is.na(show_ref)) {
    stop("`show_ref` must be TRUE or FALSE.", call. = FALSE)
  }

  invisible(show_ref)
}

#' @keywords internal
#' @noRd
.message_hidden_ref_rows <- function(function_name, table_body, show_ref) {
  if (isTRUE(show_ref) || is.null(table_body) || !"ref" %in% names(table_body)) {
    return(invisible(FALSE))
  }

  if (!any(table_body$ref %in% TRUE, na.rm = TRUE)) {
    return(invisible(FALSE))
  }

  message(
    function_name,
    "(): reference rows are hidden for dichotomous/categorical variables. ",
    "To display Ref., use `show_ref = TRUE`."
  )

  invisible(TRUE)
}
#' @keywords internal
#' @noRd
.adjustment_note <- function(adjust_for) {
  adjust_for <- unname(adjust_for)

  if (length(adjust_for) == 1) {
    return(paste0("Adjusted for ", adjust_for))
  }

  if (length(adjust_for) == 2) {
    return(paste0("Adjusted for ", adjust_for[1], " and ", adjust_for[2]))
  }

  paste0(
    "Adjusted for ",
    paste(adjust_for[-length(adjust_for)], collapse = ", "),
    ", and ",
    adjust_for[length(adjust_for)]
  )
}

#' Compose an adjustment note using displayed variable labels
#' @keywords internal
#' @noRd
.display_adjustment_note <- function(adjust_for, variable_labels = NULL) {
  display_vars <- vapply(
    adjust_for,
    .label_var,
    character(1),
    label_map = variable_labels
  )
  .adjustment_note(display_vars)
}

#' @keywords internal
#' @noRd
.interaction_note <- function(interaction) {
  paste0("Model includes interaction term: ", interaction)
}
#' @keywords internal
#' @noRd
.n_note_multi_strata <- function(stratifier, n_by_stratum) {
  if (!length(n_by_stratum)) {
    return("N reflects complete observations used in each stratum-specific model")
  }

  parts <- paste0(
    names(n_by_stratum),
    ": N = ",
    unlist(n_by_stratum, use.names = FALSE)
  )

  paste0(
    "Complete observations included by ",
    stratifier,
    " stratum: ",
    paste(parts, collapse = "; ")
  )
}
