#' Berechnet die AHV-Rentensumme mit den 5 Schritten gemäss des Modells der EPRC.
#'
#' @param PARAM_GLOBAL un dataframe d'une seule ligne, dont nous utilisons les
#'   paramètres globaux.
#'
#' @param AHV_ABRECHNUNG data frame containing benefits values, see function
#' \code{\link{mod_input_ahv_abrechnung}}.
#'
#' @param BEVOELKERUNG data frame containing the population data \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_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}}.
#'
#' @param RENTENENTWICKLUNG data frame containing the projected growth
#'   factor for the minimal benefits, see function
#'   \code{\link{mod_rentenentwicklung}}.
#'
#' @references \href{https://www.bsv.admin.ch/dam/bsv/fr/dokumente/ahv/finanzperspektiven/validierung-modellansatz-ahv.pdf.download.pdf/2018_07_09_definitif_ld_rapport_ofas.pdf}{Rapport de Prof. Dr Laurent Donzé}
#'
#' @return a `tidylist` containing the following tidy data frames:
#'  - `AHV_RENTENSUMME`
#'  - `WF_EPRC`
#'  - `FACTEUR_CORR_ANT_AJO`
#'  - `ANT_TOTAL`
#'  - `AJO_TOTAL`
#'
#' @author [MAS BSV](mailto:sekretariat.mas@bsv.admin.ch)
#'
#' @export

mod_ahv_rentensumme <- function(PARAM_GLOBAL,
                                AHV_ABRECHNUNG,
                                BEVOELKERUNG,
                                RENTENENTWICKLUNG,
                                TAUX_MORTALITE,
                                RR_AVS,
                                RENTENZYKLUS) {
    
  print("Run module: mod_ahv_rentensumme")
  
  # Berechnung der Rentenbestands-Wachstumsraten ab dem letzten Eintrag des 
  # Rentenregisters.
  WF_EPRC <-
    mod_wf_eprc(PARAM_GLOBAL,
                BEVOELKERUNG,
                TAUX_MORTALITE,
                RR_AVS)

  # Jährliche Rentensummen.
  tl <- 
    mod_ahv_rentensumme_go(PARAM_GLOBAL,
                           WF_EPRC,
                           AHV_ABRECHNUNG,
                           RENTENENTWICKLUNG,
                           RR_AVS,
                           RENTENZYKLUS)

  AHV_RENTENSUMME_GO_OLD <- tl$RENTENSUMME_AHV_GO %>%
    select(jahr, rentensumme) %>%
    rename(rent_tot = rentensumme)

  AHV_RENTENSUMME_PARAGESEXDOMNAT <- tl$RENTENSUMME_AHV_GO_PARAGESEXDOMNAT
  AHV_RENTENSUMME <- AHV_RENTENSUMME_GO_OLD

  return(list(
      AHV_RENTENSUMME = AHV_RENTENSUMME,
      AHV_RENTENSUMME_GO_OLD = AHV_RENTENSUMME_GO_OLD,
      AHV_RENTENSUMME_PARAGESEXDOMNAT = AHV_RENTENSUMME_PARAGESEXDOMNAT,
      WF_EPRC = WF_EPRC))
}
