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

test_that("regulation_matrix works", {
  scenario_example <- create_scenario(system.file("input_1culture_2pop.xlsx", package = "ambre"))

  scenario_exposure <- scenario_example |> mutate(
     volume = map(
         .x = config,                                         
         .f = ~ simulate_exposure(config = .x)))
  
  scenario_patho <- update_pathogen(scenario = scenario_exposure, pathoName = c("Rotavirus", "Campylobacter jejuni"))
  concentration = map(.x = scenario_patho$config,
                     .f= ~ simulate_inflow(.x))
  scenario_concentration <- scenario_patho |> mutate(inflow_concentration = 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 <- purrr::pmap(
    list(
      config = scenario_barrier$config,
      barrierID = scenario_barrier$SupplementaryProcessID,
      cropHeight = scenario_barrier$CropHeight,
      matrixID = scenario_barrier$MatrixID,
      popID = scenario_barrier$PopulationID,
      decay = scenario_barrier$nb_day_decay
    ),
    function(config, barrierID, cropHeight, matrixID, popID, decay) {
  
      if (barrierID %in% c(24, 26, 27, 28) &&
          matrixID %in% c(3, 5)) {
  
        update_logreduction_specific_barrier(
          config = config,
          cropHeight = cropHeight,
          barrierID = barrierID
        )
  
      } else if (barrierID %in% c(12)) {
  
        update_logreduction_decay(
          config = config,
          cropHeight = cropHeight,
          barrierID = barrierID,
          popID = popID,
          nb_day_decay = decay
        )
  
      } else if (barrierID %in% c(9,13,14,15,16,17,18,24,26,27,28)) {
  
        update_logreduction_path(
          config = config,
          matrixID = matrixID,
          barrierID = barrierID,
          popID = popID
        )
  
      } else {
        config
      }
    }
  )
    
  scenario_logreduction <- scenario_barrier |> 
      mutate(log_reduction = map(config, simulate_treatment))
  
  regulation_reduction <- config_ambre$regulation$regulation_value |> dplyr::filter(Country == "France") |>
                          dplyr::select(-c(Concentration, Country, RegulationID))
  result <- regulation_matrix_log(scenario = scenario_logreduction,
                                  regulation = regulation_reduction)
  expect_equal(object = names(result),
               expected = c("Crop name", "Target water class", "Bacteria", "Viruses"))
  
  expect_error(object = regulation_matrix_log(scenario = scenario_barrier,
                                              regulation = "France"),
               regexp = "The log_reduction column is missing from the scenario object; run simulate_treatment function to create it")
})
