#' @title Berechnung der AHV-Ausgaben
#'
#' @description Berechnet die Ausgabenkomponenten der AHV sowie die totale
#'   Ausgabensumme
#'
#' @param PARAM_GLOBAL a single row data frame, of which we use the following
#'   parameters:
#'   - `jahr_beginn`: Erstes Jahr Berechnungen
#'   - `jahr_ende`: Letztes Jahr der Projektionen
#'
#' @param AHV_ABRECHNUNG data frame containing btr_bund till jahr_abr, see function
#' \code{\link{mod_input_ahv_abrechnung}}.
#'
#' @param RENTENENTWICKLUNG data frame containing the projected growth
#'   factor for the minimal benefits, see function
#'   \code{\link{mod_rentenentwicklung}}.
#'
#' @param ECKWERTE_EXTENDED data frame containing the extended eckwerte, see function
#'   \code{\link{mod_eckwerte}}.
#'
#' @param BEVOELKERUNG data frame containing the population \code{\link{mod_population}}.
#'
#' @param MORTALITE data frame containing the mortality rates, see function
#' \code{\link{mod_input_mortalite}}.
#'
#' @param ANT_AJO_FLEX data frame containing the anticipation/ajournement rates,
#' see function \code{\link{mod_input_ant_ajo_flex}}.
#'
#' @param RR_AVS data frame containing the data coming from the rents register,
#' see function \code{\link{mod_input_rr_avs_dataframe}} and
#' create_rr_avs (dinput).
#'
#' @param CUMPROD_PARAM_ERSTRENTE data frame containing the correction factors
#' for the level of the first rents, see function
#' \code{\link{mod_scenario_erstrenten}}.
#'
#'
#' @return a `tidylist` containing the following tidy data frames:
#'   - `AHV_AUSGABEN`
#'   - `AHV_RENTENSUMME`
#'   - `EPRC_ESTIMATION`
#'   - `FACTEURS_CROISSANCE_ANNUELS_EPRC_ESTIMES`
#'   - `EPRC_PROJECTION_FLEX`
#'   - `FACTEURS_CROISSANCE_EPRC_PROJETES_FLEX`
#'   - `FACTEUR_CORR_ANT_AJO`
#'   - `ANT_TOTAL`
#'   - `AJO_TOTAL`
#'   - `AHV_AUS_UEBR`
#'
#' @author [MAS BSV](mailto:sekretariat.mas@bsv.admin.ch)
#'
#' @export

#
mod_ahv_ausgaben <- function(PARAM_GLOBAL,
                             AHV_ABRECHNUNG,
                             RENTENENTWICKLUNG,
                             ECKWERTE_EXTENDED,
                             BEVOELKERUNG,
                             TAUX_MORTALITE,
                             ANT_AJO_FLEX,
                             RR_AVS,
                             CUMPROD_PARAM_ERSTRENTE
                             ) {
    
    print("Run module: mod_ahv_ausgaben")

  # --- Berechnen der AHV-Ausgaben ---------------------------------------------
  # Rentensumme

  tl_mod_ahv_rentensumme <- mod_ahv_rentensumme(
      PARAM_GLOBAL = PARAM_GLOBAL,
      AHV_ABRECHNUNG = AHV_ABRECHNUNG,
      BEVOELKERUNG = BEVOELKERUNG,
      RENTENENTWICKLUNG = RENTENENTWICKLUNG,
      TAUX_MORTALITE = TAUX_MORTALITE,
      ANT_AJO_FLEX = ANT_AJO_FLEX,
      RR_AVS = RR_AVS,
      CUMPROD_PARAM_ERSTRENTE = CUMPROD_PARAM_ERSTRENTE
  )

  AHV_RENTENSUMME <- tl_mod_ahv_rentensumme$AHV_RENTENSUMME

  # Hilfslosenenschaedigungen

  AHV_HE <- mod_ahv_he(
      PARAM_GLOBAL = PARAM_GLOBAL,
      AHV_ABRECHNUNG = AHV_ABRECHNUNG,
      AHV_RENTENSUMME = AHV_RENTENSUMME
  )


  # Uebrige Ausgaben

  tl_mod_ahv_uebrige_ausgaben <- mod_ahv_uebrige_ausgaben(
      PARAM_GLOBAL = PARAM_GLOBAL,
      AHV_ABRECHNUNG = AHV_ABRECHNUNG,
      AHV_RENTENSUMME = AHV_RENTENSUMME,
      ECKWERTE_EXTENDED = ECKWERTE_EXTENDED,
      RENTENENTWICKLUNG = RENTENENTWICKLUNG
  )

  AHV_AUS_UEBR <- tl_mod_ahv_uebrige_ausgaben$AHV_AUS_UEBR

  # TOT_AUSG <- read.csv(paste0(PARAM_GLOBAL$ausgabe_basis_modell), sep = ";") %>%
  #   select(jahr, aus_tot = ausbagen_ohne_13) %>%
  #   mutate (aus_tot = aus_tot * 1e6) %>%
  #   filter(jahr > PARAM_GLOBAL$jahr_abr &
  #            jahr <= PARAM_GLOBAL$jahr_ende) 
  
  # Zusammenfuehren der Ausgabenkomponenten und Berechnung des Totals
  AHV_AUSGABEN <- data.frame(jahr = PARAM_GLOBAL$jahr_beginn:PARAM_GLOBAL$jahr_ende) %>%
    left_join(AHV_RENTENSUMME, by = "jahr") %>%
    left_join(AHV_HE, by = "jahr") %>%
    left_join(AHV_AUS_UEBR, by = "jahr") %>%
    mutate(aus_tot = rent_tot + he + aus_uebr)

  # --- Output -----------------------------------------------------------------

  c(
    tl_mod_ahv_rentensumme,
    tl_mod_ahv_uebrige_ausgaben,
    list(
      AHV_AUSGABEN = AHV_AUSGABEN
    )
  )
}
