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

#' Update concentration with desired value
#' 
#' Update the min, max and distribution law of the concentration in the simulated configuration according to a value defined by the user
#' 
#' @param scenario data.frame
#' @param concentration data.frame of pathogenName, min, max and distribution law to simulate
#' @importFrom purrr map
#' @importFrom dplyr mutate select
#' @return scenario_updated
#' 
#' @export
#' @examples
#' scenario_test <- create_scenario(
#'   system.file("input_1culture_2pop.xlsx", package = "ambre")
#' )
#'
#' pathogenName <- c("Norovirus", "Rotavirus")
#' concentration_custom <- data.frame(PathogenName = pathogenName,
#'                                    min = c(1, 1),
#'                                    max = c(2,2),
#'                                    type = c("uniform", "uniform"))
#'
#' scenario_updated_patho <- update_pathogen(scenario = scenario_test, pathoName = pathogenName)
#' result <- update_concentration(scenario = scenario_updated_patho ,
#'                      concentration = concentration_custom)
#'
#' dplyr::filter(result$config[[1]]$inflow, PathogenName == "Norovirus")
#'
update_concentration <- function(scenario, concentration){
  
  if (isFALSE("min" %in% names(concentration))){
    stop("List of concentration must contain min values")
  }
  
  if (isFALSE("max" %in% names(concentration))){
    stop("List of concentration must contain max values")
  }
  
  if (isFALSE("type" %in% names(concentration))){
    stop("List of concentration must contain type distribution")
  }
  
  if (isFALSE("PathogenName" %in% names(concentration))){
    stop("List of concentration must contain PathogenName")
  }
  
  ### Jointure avec nom pathggen 
  scenario_updated <- scenario 
  
  
  scenario_updated$config <- map(
  scenario_updated$config,
  \(config) {
    config$inflow <- config$inflow |>
      left_join(
        concentration,
        by = "PathogenName"
      ) |>
      mutate(
        min = coalesce(min.y, min.x),
        max = coalesce(max.y, max.x),
        type = coalesce(type.y, type.x)
      ) |>
      select(-min.x, -min.y, -max.x, -max.y, -type.x, -type.y)

    config
  }
)
  return(scenario_updated)
}
