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

#' Barrier Cost Calculation
#' 
#' Calculate cost of supplementary barriers
#' @param scenario data.frame scenario with irrigation_need_m3an column obtained from irrigation_need_calculation function
#' @param membership_fee numeric annual membership fee
#' @param charge numeric water charge
#' @param initialSituation boolean TRUE to simulate initial simulation cost, FALSE to simulate supplementary process cost
#' @importFrom dplyr rename left_join filter mutate inner_join case_when
#' @importFrom tidyr unnest
#' @return scenario_updated
#' 
#' @export
#' @examples
#' scenario_apprentissage <- create_scenario(
#'   system.file("input_1culture_2pop.xlsx", package = "ambre"))
#' irrigation_need <- irrigation_need_calculation(scenario_apprentissage)
#' process_cost_calculation(scenario = irrigation_need,
#'                          membership_fee = 200,
#'                          charge = 0.1,
#'                          initialSituation = TRUE)
#'
#'
#' process_cost_calculation(scenario = irrigation_need,
#'                          membership_fee = 200,
#'                          charge = 0.1,
#'                          initialSituation = FALSE)
process_cost_calculation <- function(scenario, membership_fee, charge, initialSituation){
 
  scenario_updated <- scenario
  perimeter_calc <- perimeter_calculation(scenario)
  scenario_updated$perimeter <- perimeter_calc$perimeter
  
  if(initialSituation == TRUE){
    supplementary_cost_capex <- scenario_updated |>  rename("TreatmentID" = "InitialProcessID") |> 
                                      unnest(TreatmentID) |>
                                      left_join(config_ambre$economic$cost, by = "TreatmentID", 
                                                relationship = "many-to-many") |> 
                                      filter(CostType == "capex" | is.na(CostType))|> 
                                      mutate("capex" = case_when(
                                                  unit == "\u20ac/ha" ~ value * Area,
                                                  unit == "\u20ac/ml" ~ value * perimeter,
                                                  unit == "\u20ac/p" ~ value * nb_population,
                                                  unit == "\u20ac"   ~ value,
                                                  unit == "\u20ac/m3"   ~ value * irrigation_need_m3an,
                                                  TRUE ~ NA_real_)) |>
                                      group_by(CropName, PopulationName, PathName) |>
                                        summarise(
                                          capex = sum(capex, na.rm = TRUE),
                                          .groups = "drop"
                                        )
    scenario_updated$capex_supplementaryProcess <- supplementary_cost_capex$capex
    scenario_updated$capex_supplementaryProcess_20y <- scenario_updated$capex_supplementaryProcess/20
    
    supplementary_cost_opex <- scenario_updated |>  rename("TreatmentID" = "InitialProcessID") |> 
                                      unnest(TreatmentID) |>
                                      left_join(config_ambre$economic$cost, by = "TreatmentID",
                                                relationship = "many-to-many") |> 
                                      filter(CostType == "opex" | is.na(CostType)) |> 
                                      mutate("opex" = case_when(
                                                  unit == "\u20ac/ha" ~ value * Area,
                                                  unit == "\u20ac/ml" ~ value * perimeter,
                                                  unit == "\u20ac/p" ~ value * nb_population,
                                                  unit == "\u20ac"   ~ value,
                                                  unit == "\u20ac/m3/y"   ~ value * irrigation_need_m3an,
                                                  TRUE ~ NA_real_)) |>
                                      group_by(CropName, PopulationName, PathName) |>
                                        summarise(
                                          opex = sum(opex, na.rm = TRUE),
                                          .groups = "drop"
                                        )
    
    scenario_updated$opex_supplementaryProcess <- supplementary_cost_opex$opex
  }
  
  else if(initialSituation == FALSE){
    supplementary_cost_capex <- scenario_updated |>  rename("TreatmentID" = "SupplementaryProcessID") |> 
                                      unnest(TreatmentID) |>
                                      left_join(config_ambre$economic$cost, by = "TreatmentID", 
                                                relationship = "many-to-many") |> 
                                      filter(CostType == "capex" | is.na(CostType))|> 
                                      mutate("capex" = case_when(
                                                  unit == "\u20ac/ha" ~ value * Area,
                                                  unit == "\u20ac/ml" ~ value * perimeter,
                                                  unit == "\u20ac/p" ~ value * nb_population,
                                                  unit == "\u20ac"   ~ value,
                                                  unit == "\u20ac/m3"   ~ value * irrigation_need_m3an,
                                                  TRUE ~ NA_real_))|>
                                      group_by(CropName, PopulationName, PathName) |>
                                        summarise(
                                          capex = sum(capex, na.rm = TRUE),
                                          .groups = "drop"
                                        )
    scenario_updated$capex_supplementaryProcess <- supplementary_cost_capex$capex
    scenario_updated$capex_supplementaryProcess_20y <- scenario_updated$capex_supplementaryProcess/20
    
    supplementary_cost_opex <- scenario_updated |>  rename("TreatmentID" = "SupplementaryProcessID") |> 
                                      unnest(TreatmentID) |>
                                      left_join(config_ambre$economic$cost, by = "TreatmentID",
                                                relationship = "many-to-many") |> 
                                      filter(CostType == "opex" | is.na(CostType)) |> 
                                      mutate("opex" = case_when(
                                                  unit == "\u20ac/ha" ~ value * Area,
                                                  unit == "\u20ac/ml" ~ value * perimeter,
                                                  unit == "\u20ac/p" ~ value * nb_population,
                                                  unit == "\u20ac"   ~ value,
                                                  unit == "\u20ac/m3/y"   ~ value * irrigation_need_m3an,
                                                  TRUE ~ NA_real_))|>
                                      group_by(CropName, PopulationName, PathName) |>
                                        summarise(
                                          opex = sum(opex, na.rm = TRUE),
                                          .groups = "drop"
                                        )
    
    scenario_updated$opex_supplementaryProcess <- supplementary_cost_opex$opex
  }

  return(scenario_updated)
    
}
