#' @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 RR_AVS data frame containing the data coming from the rents register,
#' see function \code{\link{mod_input_rr_avs_dataframe_agecon}} and
#' create_rr_avs (dinput).
#'
#' @param RENTENZYKLUS data frame containing the correction factors
#' for the level of the first rents, see function
#' \code{\link{mod_rentenzyklus}}.
#'
#'
#' @return a `tidylist` containing the following tidy data frames:
#'   - `AHV_AUSGABEN`
#'   - `AHV_RENTENSUMME`
#'   - `EPRC_ESTIMATION`
#'   - `FACTEURS_CROISSANCE_ANNUELS_EPRC_ESTIMES`
#'   - `EPRC_PROJECTION_FLEX`
#'   - `WF_EPRC`
#'   - `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,
                             RENTENZYKLUS) {
    
  print("Run module: mod_ahv_ausgaben")

  # Berechnung 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,
      RR_AVS            = RR_AVS,
      RENTENZYKLUS      = RENTENZYKLUS)

  AHV_RENTENSUMME <- tl_mod_ahv_rentensumme$AHV_RENTENSUMME

  # Hilflosenentschädigungen.
  AHV_HE <- mod_ahv_he(
      PARAM_GLOBAL    = PARAM_GLOBAL,
      AHV_ABRECHNUNG  = AHV_ABRECHNUNG,
      AHV_RENTENSUMME = AHV_RENTENSUMME)

  # Übrige 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
  
  # Zusammenführen 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)

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