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

#' Create Exposure Scenario
#'
#' This function creates exposure scenarios during the two game turns.
#'
#' @param filepath Path to the Excel file containing input data
#' @param config list config file default value config_ambre
#' @importFrom readxl read_excel
#' @importFrom dplyr mutate pick
#' @importFrom purrr map
#' @return A list containing the scenario data and a placeholder for future data
#' @export
#' @examples
#' scenario <- create_scenario(system.file("input_cas_apprentissage_complet.xlsx", package = "ambre"))
create_scenario <- function(filepath, config = config_ambre) {
  if (!is.character(filepath)) {
    stop("filename must be a string")
  }
  if (!file.exists(filepath)) {
    stop("filename must exist")
  }
  
  # load user input
  user_input <- read_excel(filepath)
  
  
  if (!is.numeric(user_input$Area)) {
    stop("Area must be a numeric value")
  }
  
  if (!is.numeric(user_input$nb_population)) {
    stop("nb_population must be a numeric value")
  }
  
  if (any(user_input$Area <= 0) || any(user_input$Area >= 10000)) {
    stop("Area must be strictly greater than 0 ha and smaller than 10,000 ha")
  }
  
  if (any(user_input$nb_population <= 0) ||
      any(user_input$nb_population >= 5000)) {
    stop("nb_population must be strictly greater than 0 and smaller than 5,000")
  }
  
  # add config file to each line
  scenario <- user_input |>  mutate(config = list(config))
  
  # retrieve ID
  cropID <- as.numeric(map(.x = scenario$CropName,
                .f = ~ query_crop(cropName = .x)))
  cropHeight <- as.numeric(map(.x = scenario$CropName,
                .f = ~ query_crop_height(cropName = .x)))
  popID <- as.numeric(map(.x = scenario$PopulationName,
               .f = ~ query_pop(popName = .x)))
  pathID <- as.numeric(map(.x = scenario$PathName,
                .f = ~ query_exp_path(pathName = .x)))
  matrixID <- as.numeric(map(.x = scenario$PathName,
                  .f = ~ query_matrix(pathName = .x)))
  STEPtttID <- map(.x = scenario$STEPtreatmentName,
                   .f = ~ query_barrier(barrierName = .x))
  collectiveTreatmentID <- map(
    .x = scenario$CollectiveTreatmentName,
    .f = ~ query_barrier(barrierName = .x)
  )
  initialProcessID <- map(
    .x = scenario$InitialProcessName,
    .f = ~ query_barrier(barrierName = .x)
  )
  supplementaryProcessID <- map(
    .x = scenario$SupplementaryProcessName,
    .f = ~ query_barrier(barrierName = .x)
  )
  
  scenario <- scenario |> mutate(
    CropID = cropID,
    CropHeight = cropHeight,
    PopulationID = popID,
    PathID = pathID,
    MatrixID = matrixID,
    STEPtreatmentID = STEPtttID,
    CollectiveTreatmentID = collectiveTreatmentID,
    InitialProcessID = initialProcessID,
    SupplementaryProcessID = supplementaryProcessID
  )
  
  scenario <- scenario |> mutate(
    InitialTrainName = pmap(
      pick(
        STEPtreatmentName,
        CollectiveTreatmentName,
        InitialProcessName
      ),
      ~ unname(
        trimws(
          unlist(
            strsplit(
              c(...)[!is.na(c(...))],
              ","
            )
          )
        ) |>
          (\(x) x[x != ""])()
      )
    )
  )
  
  scenario <- scenario |> mutate(InitialTrainID = pmap(
    pick(
      STEPtreatmentID,
      CollectiveTreatmentID,
      InitialProcessID
    ),
    ~ unname(c(...)[!is.na(c(...))])
  ))
  
  if (all(is.na(scenario$SupplementaryProcessName))) {

  scenario <- scenario |>
    mutate(SupplementaryTrainName = NA) |>
    mutate(SupplementaryTrainID = NA)

} else {
  
    scenario <- scenario |> mutate(SupplementaryTrainName = pmap(
      pick(STEPtreatmentName, CollectiveTreatmentName, InitialProcessName, SupplementaryProcessName),
      ~ unname(
          trimws(
            unlist(
              strsplit(
                c(...)[!is.na(c(...))],
                ","
              )
            )
          ) |>
            (\(x) x[x != ""])()
        )
      )
    )
    
    scenario <- scenario |> mutate(SupplementaryTrainID = pmap(
      pick(STEPtreatmentID, CollectiveTreatmentID, InitialProcessID, SupplementaryProcessID),
      ~ unname(c(...)[!is.na(c(...))])
  ))}
  
  
  
  return(scenario)
}
