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

#' Update log reduction with specific value
#' 
#' Update log reduction value according to treatment, crop, and population simulated
#' @param config list configuration simulated
#' @param cropHeight numeric, height of th crop simulated
#' @param barrierID numeric, ID of the barrier simulated
#' @importFrom dplyr filter mutate
#' @return config_updated
#' 
#' @export
#' @examples
#' scenario_example <- create_scenario(
#'   system.file("input_1culture_2pop.xlsx", package = "ambre")
#' )
#'
#' scenario_example$config <- purrr::pmap(
#'   list(
#'     config = scenario_example$config,
#'     barrierID = scenario_example$SupplementaryProcessID,
#'     matrixID = scenario_example$MatrixID,
#'     cropHeight = scenario_example$CropHeight
#'   ),
#'   function(config, barrierID, matrixID, cropHeight) {
#'
#'     if (barrierID %in% c(24, 26, 27, 28) &&
#'         matrixID %in% c(3, 5)) {
#'
#'       update_logreduction_specific_barrier(
#'         config = config,
#'         cropHeight = cropHeight,
#'         barrierID = barrierID
#'       )
#'
#'     } else {
#'       config
#'     }
#'   }
#' )
update_logreduction_specific_barrier <- function(config, cropHeight, barrierID){
  reduc_log_spe <- filter(config_ambre$treatment$barrier_specific, 
                                   TreatmentID %in% barrierID,
                                    CropHeight %in% cropHeight)
  
  barrier_modif <- filter(
    config$treatment$processes,
    TreatmentID %in% reduc_log_spe$TreatmentID
  )
  
  
  barrier_modif_updated <- mutate(
    barrier_modif,
    min = reduc_log_spe$min,
    max = reduc_log_spe$max
  )
  
  config_updated <- config
  processes <- config_updated$treatment$processes
  
  idx <- match(
        paste(processes$TreatmentID, processes$PathogenGroup),
        paste(barrier_modif_updated$TreatmentID, barrier_modif_updated$PathogenGroup)
      )
  
  sel <- !is.na(idx)
  
  processes$min[sel] <- barrier_modif_updated$min[idx[sel]]
  processes$max[sel] <- barrier_modif_updated$max[idx[sel]]
  
  config_updated$treatment$processes <- processes
  return(config_updated)
}
