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

#' Total investment cost
#' 
#' Calculates the total investment required to implement risk management processes relating to the reuse of wastewater for each simulated configuration 
#' @param scenario data.frame scenario obtained with create_scenario function
#' @param price_per_m3 numeric, water price in euro per cubic meter
#' @param membership_fee numeric annual membership fee in euro per hectare
#' @param grant numeric amount of the grant in percentage
#' @param allocation_key numeric allocation_key, NULL by default
#' @return result list
#' 
#' @export
#' @examples
#' scenario_apprentissage <- create_scenario(
#'   system.file("input_1culture_2pop.xlsx", package = "ambre")
#' )
#'
#' total_investment_cost(scenario = scenario_apprentissage,
#'                       price_per_m3 = 0.1, 
#'                       membership_fee = 200, 
#'                       grant = 0.5)
#'
#'
#' crop <- scenario_apprentissage$CropName
#' allocation_custom <- data.frame(CropName = crop,
#'                                 allocation = c(0.5, 0.5))
#' total_investment_cost(scenario = scenario_apprentissage,
#'                       price_per_m3 = 0.1, 
#'                       membership_fee = 200, 
#'                       grant = 0,
#'                       allocation_key = allocation_custom)
total_investment_cost <- function(scenario, price_per_m3, membership_fee, grant, allocation_key = NULL){
  irrigation_need <- irrigation_need_calculation(scenario)

# 1. Water price

irrigation_need_with_waterprice <- water_price(scenario = irrigation_need,
            price_per_m3 = price_per_m3,
            membership_fee = membership_fee)


# 2. Mesure collective
if (all(!is.na(irrigation_need_with_waterprice$CollectiveTreatmentID))) {
    if (is.null(allocation_key)){
      irrigation_need_with_collectivecost <- collective_treatment_cost_calculation(
                        scenario = irrigation_need_with_waterprice,
                        grant = grant
                        )
    } else {
      irrigation_need_with_collectivecost <- collective_treatment_cost_calculation(
                        scenario = irrigation_need_with_waterprice,
                        grant = grant,
                        allocation_key = allocation_key)
    }
  
} else {
  irrigation_need_with_collectivecost <- list(scenario = irrigation_need_with_waterprice,
                                                    allocation_key = NA)
  irrigation_need_with_collectivecost$scenario <-
  irrigation_need_with_collectivecost$scenario %>%
  mutate(
    capex_collective_treatment = NA,
    collective_opex = NA,
    loan_asa = NA
  )
}


# 3. Mesure individuelle : opex et 
irrigation_need_with_mesurecost <- process_cost_calculation(scenario = irrigation_need_with_collectivecost$scenario, 
                                                            membership_fee = membership_fee, 
                                                            charge = price_per_m3,
                                                            initialSituation = FALSE)
total_investment <- irrigation_need_with_mesurecost |>
  select(c(CropName, capex_supplementaryProcess, capex_collective_treatment)) |>
  rename("Investment collective process" = capex_collective_treatment,
         "Investment individual process" = capex_supplementaryProcess) |>
  pivot_longer(cols = !CropName, names_to = "cost_type", values_to = "value") |>
  unique()

  plot <- ggplot(total_investment, aes(x = CropName, 
                                 y = value, 
                                 fill = cost_type, 
                                 label = value))+
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(
    values = c(
      "Investment collective process" = "#d28433",
      "Investment individual process" = "#167779"
    )
  )+ 
  labs(fill = "Cost Type", y = "Total cost (EURO)") + 
  theme_minimal() + 
  theme(legend.position = "bottom")
  
  result <- list(plot_total_cost = plot,
                 allocation_key = irrigation_need_with_collectivecost$allocation_key)
  return(result)
    
}
