# WARNING - Generated by {fusen} from dev/flat_simulate_treatment.Rmd: do not edit by hand # nolint: line_length_linter.

#' Simulate treatment data for pathogen processes
#'
#' This function simulates treatment data for a set of pathogen processes, including
#' random values for each process and optional parameters. It can be used to generate
#' synthetic data for analysis or testing.
#' 
#' This function does the same thing as kwb.qmra::get_treatment_data(),
#' but it is bundled with *ambre* to avoid a hard dependency on the 
#' unmaintained package.
#'
#' @param processes A data frame containing information about pathogen processes,
#'   including treatment names and pathogen groups.
#' @param n_repeatings The number of times each process should be repeated.
#' @param n_events The number of events to simulate for each process.
#' @param debug Logical indicating whether to print debug information.
#' @param include_paras Logical indicating whether to include parameter data.
#' 
#' @return If `include_paras` is FALSE, returns a data frame of simulated events.
#'   If `include_paras` is TRUE, returns a list containing both the events data frame
#'   and a data frame of parameters.
#'
#' @references
#'   Sonnenberg H (2024). kwb.utils: General Utility Functions Developed at KWB. R package version 0.15.0
#'   URL: \url{https://github.com/kwb-r/kwb.utils}
#'   (MIT Licence)
#' 
#' @export
#' @examples
#' # Example usage (assuming 'processes' is defined)
#' scenario_test <- create_scenario(
#'   system.file("input_1culture_2pop.xlsx", package = "ambre")
#' )
#' scenario_updated <- update_pathogen(scenario = scenario_test,
#'  pathoName = c("Campylobacter jejuni", "Rotavirus"))
#' config = scenario_updated$config[[1]]
#' processes_simulated <- config$treatment$processes
#'
#' result <- get_treatment_data(processes_simulated, n_repeatings = 10, n_events = 5)
#' print(result)
get_treatment_data <- function(
  processes, n_repeatings, n_events, debug = TRUE, include_paras = TRUE
)
{
  # Helper function to select a treatment from "processes" and report about it
  get_treatment <- function(i) {
    treatment <- processes[i, ]
    if (debug) cat(sprintf(
      "Simulated treatment: %s for %s\n", 
      treatment$TreatmentName, 
      treatment$PathogenGroup
    ))
    treatment[, c("TreatmentID", "PathogenGroup")]
  }
  
  # Provide row indices
  indices <- seq_len(nrow(processes))
  
  # Create random values for each process
  random_list <- lapply(indices, function(i) {
    generate_random_values(
      config = processes[i, ], 
      number_of_repeatings = n_repeatings,
      number_of_events = n_events,
      debug = debug
    )
  })

  # Function to cbind random events and treatment
  cbind_events_treatment <- function(i) {
    cbind(random_list[[i]]$events, get_treatment(i), row.names = NULL)
  }
  
  # Build data frame "events"
  events <- lapply(indices, cbind_events_treatment) |>
    do.call(what = rbind) |>
    renameColumns(list(values = "logreduction"))
  
  # Return the events if parameters are not requested
  if (! include_paras) {
    return(events)
  }
  
  # Function to cbind random events and treatment
  cbind_paras_treatment <- function(i) {
    cbind(random_list[[i]]$paras, get_treatment(i), row.names = NULL)
  }
  
  # Return a list with events and parameters
  list(
    events = events,
    paras = lapply(indices, cbind_paras_treatment) |>
      do.call(what = plyr::rbind.fill)
  )
}
