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

#' QMRA on learning case
#' 
#' Run a learning case to evaluate risk on scenario with 6 crops (Corn Seed, Tomato, Potato, Salad, Onion) and 6 populations (maintainer, harverster, irrigation, consumer, local resident, passerby). Run QMRA calculation with barrier measures
#' @importFrom purrr map pmap
#' @importFrom dplyr mutate filter select
#' @return all_plots list
#' 
#' @export
#' @examples
#' \donttest{
#' run_qmra_learning_case()
#' }
run_qmra_learning_case <- function(){
  scenario_example <- create_scenario(system.file("input_cas_apprentissage_complet.xlsx", package = "ambre"))


  frequency_up <- c(3L, 12L, 20L, 20L, 3L, 10L, 20L, 20L, 48L,
                    3L, 20L, 20L, 96L, 3L, 48L, 12L, 20L, 96L,
                    3L, 48L, 48L, 20L, 48L, 3L, 48L, 48L, 48L)
  scenario_frequency <- update_frequency_with_desired_value(scenario = scenario_example,                                                          frequency = frequency_up)
  
  volumes_up <- data.frame("min" = c(0.05,0.001,0.00174,0.001,0.03,0.0001,0.0008,0.001,0.009,0.03,0.0008,0.001,0.006,0.01,0.001,0.001,0.0008,0.00375,0.01,0.001,0.001,0.0008,0.015,0.005,0.001,0.001,0.0025), 
                           "max" = c(0.05,0.001,0.00174,0.001,0.03,0.0001,0.0008,0.001,0.009,0.03,0.0008,0.001,0.006,0.01,0.001,0.001,0.0008,0.00375,0.01,0.001,0.001,0.0008,0.015,0.005,0.001,0.001,0.0025),
                           "type" = rep("triangle", nrow(scenario_example)))
  scenario_volume <- update_volume_with_desired_value(scenario = scenario_frequency,                                                    volume = volumes_up)
  scenario_exposure <- scenario_volume |> mutate(
       volume = map(
           .x = config,                                         
           .f = ~ simulate_exposure(config = .x)))
  plot_volume <- plot_volumes(scenario_exposure)
  
  scenario_patho <- update_pathogen(scenario = scenario_exposure, pathoName = c("Rotavirus"))
  concentration = map(.x = scenario_patho$config,
                     .f= ~ simulate_inflow(.x))
  scenario_concentration <- scenario_patho |> mutate(inflow_concentration = concentration)
  plot_inflow <- plot_inflow(scenario_concentration)
  
  scenario_ini_dose <- initial_dose_calculation(scenario = scenario_concentration)
  
  scenario_barrier <- update_treatment_scheme(scenario = scenario_ini_dose, initial_situation = TRUE)
  scenario_barrier$config <- pmap(
    list(
      config = scenario_barrier$config,
      barrierID = scenario_barrier$InitialProcessID,
      cropHeight = scenario_barrier$CropHeight,
      matrixID = scenario_barrier$MatrixID,
      popID = scenario_barrier$PopulationID,
      decay = scenario_barrier$nb_day_decay
    ),
    retrieve_specific_barrier
  )
  
  scenario_logreduction <- scenario_barrier |> 
    mutate(log_reduction = map(config, simulate_treatment))
  plot_log_reduc <- plot_logreduction(scenario=scenario_logreduction)
  
  regulation_concentration <- config_ambre$regulation$regulation_value |> filter(Country == "France") |>
                        select(-c(Country, RegulationID, Reduction))
  regulation_reduction <- config_ambre$regulation$regulation_value |> filter(Country == "France") |>
                        select(-c(Concentration, Country, RegulationID))
  matrix_log <- regulation_matrix_log(scenario = scenario_logreduction,
                                      regulation = regulation_reduction)
  matrix_concentration <- regulation_matrix_concentration(scenario = scenario_logreduction,
                                                          regulation = regulation_concentration)
    
  scenario_final_dose <- final_dose_calculation(scenario_logreduction)
  scenario_inf_proba <- infection_probability_calculation(scenario_final_dose)
  scenario_illness_proba <- illness_probability_calculation(scenario_inf_proba)
  scenario_dalys <- dalys_calculation(scenario_illness_proba)
  scenario_risk_total <- get_risk_total(scenario_dalys)
  plot_dalys <- plot_dalys(scenario_risk_total)
  
  economic_result <- run_economic_analysis(scenario = scenario_risk_total,
                      price_per_m3 = 0.1, 
                      membership_fee = 200, 
                      grant = 0.5,
                      initialSituation = TRUE)
  
  all_plots <- list(
    concentration = plot_inflow,
    volume = plot_volume,
    logreduction = plot_log_reduc,
    dalys = plot_dalys,
    regul_matrix_log = matrix_log,
    regul_matrix_concentration = matrix_concentration,
    economic = economic_result
  )
  
  return(all_plots)
    
}
