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

#' Get table risk
#' 
#' Get table risk to calculate infection probability
#' @param scenario data.frame with columns inflow concentration, final dose and volume
#' @return tbl_risk
#' 
#' @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", "Escherichia coli"))
#'
#' 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)
#' tbl_risk_test <- get_tbl_risk(scenario_final_dose_and_co_test)
get_tbl_risk <- function(scenario){
  
  if (!"inflow_concentration" %in% names(scenario)) {
  stop("The inflow_concentration column is missing from the scenario object; run the inflow_concentration function to create it")
  }
  
  if (!"volume" %in% names(scenario)) {
  stop("The volume column is missing from the scenario object; run the simulate_exposure function to create it")
}
  
  if (!"final_dose" %in% names(scenario)) {
  stop("The final_dose column is missing from the scenario object; run the final_dose_calculation function to create it")
  }
 
  tbl_risk <- map(scenario$final_dose, ~.x)
  tbl_risk <- map(tbl_risk, ~rename(.x, dose_perEvent = final_dose))
  tbl_risk <- map(tbl_risk, ~rename(.x, logreduction = total_logreduction))
  tbl_risk <- map(tbl_risk, ~{
                      .x$effluent <- 10^(log10(.x$inflow) - .x$logreduction)
                      .x
                      })
  tbl_risk <- map(tbl_risk, ~{
                      .x$exposure_perEvent = .x$effluent * .x$volume_perEvent
                      .x
                      })
  
  return(tbl_risk)
}
