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

#' Plot volume
#' 
#' Boxplot of the volume of exposition for each population exposed
#' @param scenario data.frame with volume column obtained with simulate_exposure function
#' @importFrom dplyr mutate
#' @importFrom tidyr unnest
#' @import ggplot2
#' @import colorspace
#' @importFrom stringr str_wrap
#' @importFrom purrr map
#' @return plot
#' 
#' @export
#' @examples
#' library(dplyr)
#' library(purrr)
#' scenario_test <- create_scenario(system.file("input_1culture_2pop.xlsx", package = "ambre"))
#'
#' scenario_conc <- inflow_concentration(scenario = scenario_test,
#'  pathogenName = c("Campylobacter jejuni"))
#'
#' scenario_volume <-
#'   scenario_conc |>                     
#'   mutate(
#'     volume = map(
#'       .x = config,                                         
#'       .f = ~ simulate_exposure(config = .x)              
#'     )
#'   )
#'
#' plot_volumes(scenario_volume)
plot_volumes <- function(scenario){
  if (!"volume" %in% names(scenario)) {
  stop("The volume column is missing from the scenario object; run the simulate_exposure function to create it")
  }
  
  plot_vol <- scenario |> select(CropName, PopulationName, PathName) |>
            mutate(volume_perEvent = map(.x = scenario$volume, 
                                         .f = ~.x$volumes$events$volume_perEvent))


  plot_vol_long <- plot_vol |> unnest(volume_perEvent)
  
  n_path <- length(unique(plot_vol_long$PathName))

  plot <- ggplot(plot_vol_long, aes(x = PopulationName, y = volume_perEvent*1000, fill = PathName)) +
              geom_boxplot() +
              facet_grid(~CropName) +
              theme_minimal() +
              labs(x = "Population exposed",
                   y = "(mL/event)",
                   fill = "Exposition path"
              )+
              scale_fill_manual(
                values = rainbow_hcl(n_path),
                labels = \(x) str_wrap(x, width = 60)
              ) +
               guides(
                fill = guide_legend(
                  nrow = length(unique(plot_vol_long$PathName)),
                  byrow = TRUE
                )
              ) +
              theme_minimal() +
              theme(
                legend.position = "bottom",
                legend.direction = "horizontal",
                axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)
              )
  
  return(plot)
    
}
