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

#' Simulate: inflow
#'
#' This function does the same thing as kwb.utils::simulate_inflow(),
#' but it is bundled with *ambre* to avoid a hard dependency on the
#' unmaintained package.
#' 
#' Simulate pathogen concentrations
#' @param config list contained in the scenario object
#' @param debug print debug information (default: TRUE)
#' @param lean if \code{TRUE}, only the "events" are returned (in a reduced
#'   version, i.e. without column \code{PathogenName} and with all ID columns
#'   being of class \code{integer}), otherwise a list with the events in element
#'   \code{events}" and the corresponding parameters in element \code{paras}.
#'   The default is \code{FALSE}, i.e. events and parameters are returned in a
#'   list.
#' @importFrom plyr rbind.fill
#' @return list with parameters of user defined random distribution and
#'   corresponding values
#' @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_inflow
#' scenario_test <- create_scenario(system.file("input_1culture_2pop.xlsx", package = "ambre"))
#' scenario_updated <- update_pathogen(scenario = scenario_test,
#'  pathoName = c("Campylobacter jejuni", "Rotavirus"))
#'
#' # Call simulate_inflow with the sample config
#' result_1 <- simulate_inflow(config = scenario_updated$config[[1]], debug = FALSE)
#'
#' # Print the results
#' cat("Parameters used for simulation:")
#' print(result_1$paras)
#'
#' # Example with lean = TRUE
#' lean_result_1 <- simulate_inflow(config = scenario_updated$config[[1]], debug = FALSE, lean = TRUE)
#' cat("Lean result (only events):")
#' print(lean_result_1)
simulate_inflow <- function(config, debug = TRUE, lean = FALSE){
  # Only pathogens to be simulated
  patho_ids <- config$inflow$PathogenID[config$inflow$simulate == 1]

  # Helper function to select a row from config$inflow
  get_inflow_config_row <- function(patho_id) {
    inflow_config <- config$inflow[config$inflow$PathogenID == patho_id, ] 
    catIf(debug, sprintf(
      "Simulated pathogen: %s\n", inflow_config$PathogenName
    ))
    inflow_config
  }

  random_objects <- lapply(patho_ids, function(patho_id) {

    inflow_config_row <- get_inflow_config_row(patho_id)

    lapply(
      generate_random_values(
        config = inflow_config_row,
        number_of_repeatings = number_of_repeatings(config),
        number_of_events = number_of_exposures(config),
        debug = debug
      ),
      cbind,
      inflow_config_row[, c("PathogenID", "PathogenName", "PathogenGroup")]
    )
  })

  events <- catAndRun(
    "Providing inflow events",
    do.call(rbind, lapply(random_objects, "[[", "events")) |>
      renameColumns(list(values = "inflow"))
  )

  if (lean) return(
    events |>
      removeColumns("PathogenName") |>
      id_columns_to_integer()
  )

  list(
    events = events,
    paras = catAndRun(
      "Providing inflow paras",
      do.call(plyr::rbind.fill, lapply(random_objects, "[[", "paras"))
    )
  )
}
