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

#' Plot infection probability
#' 
#' Boxplot of infection probability per year according to population exposed and pathogen simulated
#' @param scenario with risk_total column obtained with get_risk_total
#' @import ggplot2
#' @importFrom tibble tibble
#' @importFrom tidyr unnest
#' @importFrom purrr map imap
#' @return plots
#' 
#' @export
#' @examples
#' \donttest{
#' 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", "Escherichia coli", "Norovirus", "Rotavirus"))
#'
#' scenario_volume <-
#'   scenario_conc |>                     
#'   mutate(
#'     volume = map(
#'       .x = config,                                         
#'       .f = ~ simulate_exposure(config = .x)              
#'     )
#'   )
#'
#' scenario_dose_ini <- initial_dose_calculation(scenario_volume)
#'
#' scenario_scheme <- update_treatment_scheme(scenario_dose_ini)
#'
#' scenario_with_logreduc_and_co <- scenario_scheme |> 
#'   mutate( log_reduction = map(config, simulate_treatment))
#'
#' scenario_final_dose_and_co_test <- final_dose_calculation(scenario_with_logreduc_and_co)
#'
#' scenario_inf_proba_and_co <- infection_probability_calculation(scenario_final_dose_and_co_test)
#'
#' scenario_illness_proba_and_co <- illness_probability_calculation(scenario_inf_proba_and_co)
#'
#' scenario_dalys_and_co <- dalys_calculation(scenario_illness_proba_and_co)
#'
#' scenario_risk_total_and_co <-get_risk_total(scenario_dalys_and_co)
#'
#' plot_infection_probability(scenario_risk_total_and_co)
#' }
plot_infection_probability <- function(scenario){
  plot_inf_proba <- tibble(
    PopulationName = scenario$PopulationName,
    CropName = scenario$CropName,
    PathogenGroup = map(scenario$risk_total, ~.x$PathogenGroup),
    PathogenName = map(scenario$risk_total, ~.x$PathogenName.x),
    infectionProb_total = map(scenario$risk_total, ~.x$infectionProb_sum))

  plot_inf_proba_long <- plot_inf_proba |> unnest(c(infectionProb_total,PathogenGroup, PathogenName))
  
  n_pathogens <- plot_inf_proba_long %>% 
    distinct(PathogenName) %>% 
    nrow()
  
  colors <- c("Tomato" = "#E53935",
              "Apple" ="#2E7D32",
              "Salad" = "#AEEA00",
              "Potato" = "#C49A6C",
              "Corn seed" = "#FDD835",
              "Onion" = "#9C27B0")
  
  
  plots <- plot_inf_proba_long |>
    split(~ PathogenName) |>
    imap(function(df, pathogen){

      ggplot(df, aes(
        x = PopulationName,
        y = infectionProb_total,
        fill = CropName
      )) +
        geom_boxplot() +
        scale_fill_manual(values = colors, name = "Crop name") +
        theme_minimal() +
        labs(x = "Population expopsed",
            y = "infection probability per year",
             title = pathogen)
    })
  
    return(plots)
    
}
