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

#' Update volume with desired value
#' 
#' Update the min and max value of the volume in the simulated configuration according to a value defined by the user
#' 
#' @param scenario data.frame
#' @param volume numeric, named list of min and max value to simulate
#' @importFrom purrr map2
#' @importFrom dplyr mutate
#' @return scenario_updated
#' 
#' @export
#' @examples
#' scenario_example <- create_scenario(
#'   system.file("input_1culture_2pop.xlsx", package = "ambre")
#' )
#' volumes <- data.frame("min" = c(1.1, 1.2), "max" = c(1.3,1.4), "type" = c("triangle", "triangle"))
#' scenario_example_updated <- update_volume_with_desired_value(scenario = scenario_example,
#'                                  volume = volumes)
#'
#' print(scenario_example_updated$config[[1]]$exposure)
#' print(scenario_example_updated$config[[2]]$exposure)
update_volume_with_desired_value <- function(scenario, volume){
  
  if (isFALSE("min" %in% names(volume))){
    stop("List of volume must contain min values")
  }
  
  if (isFALSE("max" %in% names(volume))){
    stop("List of volume must contain min values")
  }
  
  if (isFALSE("type" %in% names(volume))){
    stop("List of volume must contain type of the distribution")
  }
  
  if (isFALSE(length(volume$min == length(scenario$config)))){
    stop("A min volume value is required for all lines in the scenario")
  } 
  
  if (isFALSE(length(volume$max == length(scenario$config)))){
    stop("A max volume value is required for all lines in the scenario")
  } 
  
  
  scenario_updated <- scenario |>
    mutate(
      config = map2(
        .x = config,
        .y = volume$min,
        .f = ~{
          .x$exposure$min[[3]] <- .y
          .x                          
        }
      )
    ) |>
    mutate(
      config = map2(
        .x = config,
        .y = volume$max,
        .f = ~{
          .x$exposure$max[[3]] <- .y
          .x
        }
      )
    ) |>
    mutate(
      config = map2(
        .x = config,
        .y = volume$type,
        .f = ~{
          .x$exposure$type[[3]] <- .y
          .x
        }
      )
    )
  return(scenario_updated)
}
