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

#' Run QMRA custom 
#' 
#' Run the full QMRA workflow using custom data for volume and frequency of exposure (not the default databases)
#' 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 obtain with create_scenario function
#' @param concentration data.frame of pathogenName, min, max and distribution law to simulate
#' @param volume data.frame of min and max value, same size as scenario
#' @param frequency array of integer same size as scenario
#' @param objective numeric reference dalys threshold to compare simulation result on dalys plot. default value is OMS threshold 1e-6 dalys
#' @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
#' @param initialSituation boolean, TRUE to consider InitialTrainID for simulation, FALSE to consider SupplementaryTrainID for simulation
#' @importFrom purrr pmap map
#' @importFrom dplyr mutate
#' @return all_plots list 
#' 
#' @export
#' @examples
#' \donttest{
#' sc <- create_scenario(system.file("input_1culture_2pop.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))
#'
#' concentration_custom <- data.frame(PathogenName = c("Rotavirus"),
#'                                    min = c(1),
#'                                    max = c(2),
#'                                    type = c("uniform"))
#' run_qmra_custom(scenario = sc,
#'                   concentration = concentration_custom,
#'                   volume = data.frame(min=c(0.5,1), max = c(1,2), type= c("triangle", "triangle")),
#'                   frequency = c(200L, 300L),
#'                   regulationLog = regulation_reduction,
#'                   regulationConcentration = regulation_concentration,
#'                   initialSituation = TRUE)
#'
#' run_qmra_custom(scenario = sc,
#'                   concentration = concentration_custom,
#'                   volume = data.frame(min=c(0.5,1), max = c(1,2), type= c("triangle", "triangle")),
#'                   frequency = c(200L, 300L),
#'                   regulationLog = regulation_reduction,
#'                   regulationConcentration = regulation_concentration,
#'                   initialSituation = FALSE)
#'}
run_qmra_custom <- function(scenario,  
                              volume, 
                              frequency,
                              concentration, 
                              objective = 1e-6, 
                              regulationLog, 
                              regulationConcentration,
                              initialSituation) {
  
  
  if (initialSituation == TRUE){
    if(all(is.na(scenario$InitialTrainID))){
      stop("No value in column InitialTreatmentName of the scenario")
    }
  } else if (initialSituation == FALSE) {
    if(all(is.na(scenario$SupplementaryTrainID))){
      stop("No value in column SupplementaryTreatmentName of the scenario")
    }
  }

  sc  <- update_volume_with_desired_value(scenario = scenario, volume = volume)
  
  sc <- update_frequency_with_desired_value(scenario = sc, frequency = frequency)
  
  sc <- inflow_concentration_custom(scenario = sc, concentration = concentration)
  
  sc <- sc |> mutate(volume = map(config, ~ simulate_exposure(config = .x)))
  sc <- initial_dose_calculation(scenario = sc)
  sc <- update_treatment_scheme(scenario = sc, 
                                initial_situation = initialSituation)
  
  if (initialSituation == TRUE){
    sc$config <- pmap(
    list(
      config = sc$config,
      barrierID = sc$InitialProcessID,
      cropHeight = sc$CropHeight,
      matrixID = sc$MatrixID,
      popID = sc$PopulationID,
      decay = sc$nb_day_decay
    ),
    retrieve_specific_barrier
  )
  } else if (initialSituation == FALSE){
    sc$config <- pmap(
    list(
      config = sc$config,
      barrierID = sc$SupplementaryProcessID,
      cropHeight = sc$CropHeight,
      matrixID = sc$MatrixID,
      popID = sc$PopulationID,
      decay = sc$nb_day_decay
    ),
    retrieve_specific_barrier
  )
  }
  
  
  sc <- sc |> mutate(log_reduction = map(config, ~ simulate_treatment(.x)))
  sc <- final_dose_calculation(scenario = sc)
  sc <- infection_probability_calculation(scenario = sc)
  sc <- illness_probability_calculation(scenario = sc)
  sc <- dalys_calculation(scenario = sc)
  
  
  
  log_reduction_plot <- plot_logreduction(scenario = sc)
  
  matrix_log <- regulation_matrix_log(scenario = sc,
                                      regulation = regulationLog)
  
  matrix_concentration <- regulation_matrix_concentration(scenario = sc,
                                                          regulation = regulationConcentration)
  
  scenario_risk_total <- get_risk_total(sc)
  
  dalys_plot <- plot_dalys(scenario = scenario_risk_total,
                           objective = objective)
  
  
  
  all_plots <- list(
    logreduction = log_reduction_plot,
    dalys = dalys_plot,
    regul_matrix_log = matrix_log,
    regul_matrix_concentration = matrix_concentration
  )
  
  return(all_plots)
  
}
