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

#' Run QMRA on initial situation
#' 
#' Run the full QMRA workflow
#' 1) simulate inflow concentration, volume of exposure 
#' 2) calculate initial dose
#' 3) simulate log reduction for treatment processes
#' 4) calculate final dose
#' 5) calculate infection probability, illness probability and dalys reduction
#' 6) calculate annual total risk
#' 
#' @param scenario data.frame scenario obtained with create_scenario function path of the scenario file
#' @param pathogen character Name of the pathogen to simulate
#' @param regulationLog data.frame regulation value of the log reduction to compare with simulation result
#' @param regulationConcentration data.frame regulation value of the log reduction to compare with simulation result
#' @importFrom purrr map
#' 
#' @return all_plots list of ggplot
#' 
#' @export
#' @examples
#' \donttest{
#' library(dplyr)
#' scenario <- create_scenario(system.file("input_cas_apprentissage_complet.xlsx", package = "ambre"))
#' regulation_reduction <- config_ambre$regulation$regulation_value |> 
#'   dplyr::filter(Country == "France") |>
#'   dplyr::select(-c(Concentration, Country, RegulationID))
#' regulation_concentration <- config_ambre$regulation$regulation_value |> 
#'   dplyr::filter(Country == "France") |>
#'   dplyr::select(-c(Country, RegulationID, Reduction))
#' run_qmra_initial_situation(scenario = scenario, 
#'                    pathogen = c("Campylobacter jejuni", "Escherichia coli"),
#'                    regulationLog = regulation_reduction,
#'                    regulationConcentration = regulation_concentration)
#' }
run_qmra_initial_situation <- function(scenario, pathogen, regulationLog, regulationConcentration){
  
  scenario_concentration <- inflow_concentration(scenario = scenario,
   pathogenName = pathogen)

  scenario_volume <-
    scenario_concentration |>                     
    mutate(
      volume = map(
        .x = config,                                         
        .f = ~ simulate_exposure(config = .x)              
      )
    )

  scenario_dose_ini <- initial_dose_calculation(scenario_volume)
  
  scenario_update_ttt_scheme <- update_treatment_scheme(scenario = scenario_dose_ini, initial_situation = TRUE)
  
  scenario_update_ttt_scheme$config <- purrr::pmap(
  list(
    config = scenario_update_ttt_scheme$config,
    barrierID = scenario_update_ttt_scheme$InitialProcessID,
    cropHeight = scenario_update_ttt_scheme$CropHeight,
    matrixID = scenario_update_ttt_scheme$MatrixID,
    popID = scenario_update_ttt_scheme$PopulationID,
    decay = scenario_update_ttt_scheme$nb_day_decay
  ),
  retrieve_specific_barrier
)
  
  scenario_logreduction <- scenario_update_ttt_scheme |>
    mutate(log_reduction = map(.x = config, .f = ~ simulate_treatment(.x)))
  
  log_reduction_plot <- plot_logreduction(scenario_logreduction)
  
  matrix_log <- regulation_matrix_log(scenario = scenario_logreduction,
                                      regulation = regulationLog)
  
  matrix_concentration <- regulation_matrix_concentration(scenario = scenario_logreduction,
                                                          regulation = regulationConcentration)
  
  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)
  
  dalys_plot <- plot_dalys(scenario_risk_total)

  
  all_plots <- list(
    logreduction = log_reduction_plot,
    dalys = dalys_plot,
    regul_matrix_log = matrix_log,
    regul_matrix_concentration = matrix_concentration
  )
  
  return(all_plots)
    
}
