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

#' Annual cost
#' 
#' Calculate annual cost of the scenario simulated
#' @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 initialSituation boolean TRUE to simulate initial situation cost, FALSE to simulation supplementary process cost
#' @param allocation_key numeric allocation key, NULL by default
#' @importFrom tidyr pivot_longer
#' @import ggplot2
#' @return result list
#' 
#' @export
#' @examples
#' scenario_apprentissage <- create_scenario(
#'   system.file("input_1culture_2pop.xlsx", package = "ambre")
#' )
#' annual_cost(scenario = scenario_apprentissage,
#'             price_per_m3 = 0.1,
#'             membership_fee = 200,
#'             grant = 0.5,
#'             initialSituation = TRUE)
#'
#' annual_cost(scenario = scenario_apprentissage,
#'             price_per_m3 = 0.1,
#'             membership_fee = 200,
#'             grant = 0.5,
#'             initialSituation = FALSE)
#'
#' crop <- scenario_apprentissage$CropName
#' allocation_custom <- data.frame(CropName = crop,
#'                                 allocation = c(0.5, 0.5))
#' annual_cost(scenario = scenario_apprentissage,
#'             price_per_m3 = 0.1,
#'             membership_fee = 200,
#'             grant = 0,
#'             initialSituation = TRUE,
#'             allocation_key = allocation_custom)
#'
#' annual_cost(scenario = scenario_apprentissage,
#'             price_per_m3 = 0.1,
#'             membership_fee = 200,
#'             grant = 0,
#'             initialSituation = FALSE,
#'             allocation_key = allocation_custom)
#'
annual_cost <- function(scenario, price_per_m3, membership_fee, grant, initialSituation, allocation_key = NULL){
  irrigation_need <- irrigation_need_calculation(scenario = 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 capex
  if(initialSituation == TRUE){
    irrigation_need_with_mesurecost <- process_cost_calculation(scenario = irrigation_need_with_collectivecost$scenario, 
                                                              membership_fee = membership_fee, 
                                                              charge = price_per_m3,
                                                              initialSituation = TRUE)
  }
  else if(initialSituation == FALSE){
    irrigation_need_with_mesurecost <- process_cost_calculation(scenario = irrigation_need_with_collectivecost$scenario, 
                                                              membership_fee = membership_fee, 
                                                              charge = price_per_m3,
                                                              initialSituation = FALSE)
  }
  
  
  
  irrigation_need_long <- irrigation_need_with_mesurecost |>
    select(c(CropName, annual_membership_fee, loan_asa, 
             water_charge, collective_opex, opex_supplementaryProcess, capex_supplementaryProcess_20y)) |>
    rename("Annual membership fee" = annual_membership_fee,
           "Water price" = water_charge,
           "Operational cost collective process" = collective_opex,
           "Investment collective process (20-year loan)" = loan_asa,
           "Operational cost individual process" = opex_supplementaryProcess,
           "Investment individual process (20-year loan)" = capex_supplementaryProcess_20y)|>
    pivot_longer(cols = !CropName, names_to = "cost_type", values_to = "value") |>
    mutate(
    cost_type = factor(
      cost_type,
      levels = c(
        "Water price",
        "Annual membership fee",
        "Operational cost collective process",
        "Investment collective process (20-year loan)",
        "Operational cost individual process",
        "Investment individual process (20-year loan)"
      )
    )
  )|>
    unique()
  
  plot <- ggplot(irrigation_need_long, aes(x = CropName, 
                                   y = value, 
                                   fill = cost_type, 
                                   label = value))+
    geom_bar(stat = "identity", position = position_stack(reverse = TRUE)) +
    geom_text(position = position_stack(vjust = 0.5, reverse = TRUE))+
    scale_fill_manual(
      values = c(
        "Water price" = "palegreen4",
        "Annual membership fee" = "palegreen",
        "Operational cost collective process"  = "#fedc7f",
        "Investment collective process (20-year loan)" = "#d28433",
        "Operational cost individual process"  = "#9bdde1",
        "Investment individual process (20-year loan)" = "#167779"
      )
    )+
    labs(fill = "Cost Type", 
         y = "Annual cost (EUR/year)",
         x = "Crop name") + 
    theme_minimal() + 
    theme(legend.position = "bottom")
  
  result <- list(
    plot_annual_cost = plot,
    allocation_key = irrigation_need_with_collectivecost$allocation_key
  )
  return(result)
    
}
