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

#' Simulate Treatment
#'
#' This function simulates treatment data for pathogen processes based on the provided configuration.
#' It generates treatment events and parameters, and can return results in either long or wide format.
#' 
#' This function does the same thing as kwb.qmra::simulate_treatment(),
#' but it is bundled with *ambre* to avoid a hard dependency on the 
#' unmaintained package.
#'
#' @param config A configuration object containing treatment scheme and process information.
#' @param wide Logical indicating whether to return results in wide format.
#' @param debug Logical indicating whether to print debug information.
#' @param lean Logical indicating whether to return only essential results for web applications.
#'
#' @return If `lean` is TRUE, returns a list containing only the long-format treatment events.
#'   If `wide` is TRUE, returns a list containing long-format events, wide-format events,
#'   wide-format scheme events, treatment schemes, and treatment parameters.
#'   If `wide` is FALSE, returns a list containing long-format events, treatment schemes,
#'   and treatment parameters.
#' 
#' @importFrom dplyr filter left_join select group_by slice
#'
#' @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 of simulate_treatment
#' \donttest{
#' 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]]
#'
#' # Simulate treatment data
#' result <- simulate_treatment(config, wide = TRUE, debug = FALSE, lean = FALSE)
#'
#' }
simulate_treatment <- function(
  config, wide = FALSE, debug = TRUE, lean = FALSE
){
  inflow_config_simulated <- config$inflow |>
    dplyr::filter(.data$simulate == 1)

  schemes_config <- config$treatment$schemes

  processes_simulated <- config$treatment$processes |>
    dplyr::filter(
      .data$PathogenGroup %in% unique(inflow_config_simulated$PathogenGroup),
      .data$TreatmentID %in% unique(schemes_config$TreatmentID)
    )
  
  treatment_data <- get_treatment_data(
    processes = processes_simulated, 
    n_repeatings = number_of_repeatings(config), 
    n_events = number_of_exposures(config), 
    debug = debug,
    include_paras = ! lean
  )

  treatment_events <- treatment_data$events |>
    dplyr::left_join(schemes_config)
  
  # Return only the result that is required by the web app if "lean" is TRUE
  if (lean) {
    
    return(list(events_long = treatment_events))
  } 
  
  if (wide) {
    
    treatment_events_wide <- tidyr::spread(
      data = treatment_events,
      key = .data$TreatmentID,
      value = .data$logreduction
    )
    
    schemes_events_wide <- get_scheme_events_wide(config, treatment_events_wide)
  }
  
  # Create and return further results only if "lean" is FALSE
  treatment_id_to_name <- config$treatment$processes |>
    dplyr::select(.data$TreatmentID, .data$TreatmentName) |>
    dplyr::group_by(.data$TreatmentID) |> 
    dplyr::slice(1)
  
  treatment_paras <- treatment_data$paras |>
    dplyr::left_join(treatment_id_to_name)
  
  if (wide) list(
    
    events_long = treatment_events,
    events_wide = treatment_events_wide,
    schemes_events_wide = schemes_events_wide,
    schemes = schemes_config,
    paras = treatment_paras
    
  ) else list(
    
    events_long = treatment_events,
    schemes = schemes_config,
    paras = treatment_paras
  )
}
