#' @title Diskontierung der Ergebnisse und Berechnung der AHV-Indices
#'
#' @description Diskontiert die Ergebnisse des AHV Finanzhaushalts und berechnet
#'   die Indices
#'
#' @param PARAM_GLOBAL a single row data frame, of which we use the following
#'   parameters:
#'   - `flag_param_massn`: TRUE bedeutet Berechnung mit Massnahmen
#'
#' @param AHV_BILANZ Data frame containing the (projected) nominal Bilanz, see
#' function \code{\link{mod_ahv_bilanz}}.
#'
#' @param AHV_MASSNAHMEN Data frame containing the projected nominal financial
#'   effects of reform measures, see function \code{\link{wrap_ahv_massnahmen}}.
#'   If flag_param_massn = FALSE then empty, see function
#'   \code{run_container}.
#'
#' @param RENTENENTWICKLUNG Data frame containing the (projected)
#'   evolutions of salaries, price and mixed index (indice mixte) of the minimal
#'   OASI rent, see function \code{\link{mod_rentenentwicklung}}.
#'
#' @param DISKONTFAKTOR Data frame containing the discount factors, see function
#' \code{\link{mod_diskontfaktor}}.
#'
#' @param list List of input data frames.
#'
#' @return A `tidylist` containing the following tidy data frames:
#'   - `AHV_FHH`             containing the discounted household
#'   - `AHV_FHH_NOM`         containing the nominal household
#'   - `AHV_MASSNAHMEN_real` contains the discounted financial effects of the 
#'                           measures, if not empty
#'   - `AHV_MASSNAHMEN_nom`  contains the discounted financial effects of the 
#'                           measures, if not empty
#'
#' @author [MAS BSV](mailto:sekretariat.mas@bsv.admin.ch)
#'
#' @export


mod_ahv_postprocessing <- function(PARAM_GLOBAL,
                                   AHV_BILANZ,
                                   AHV_MASSNAHMEN,
                                   RENTENENTWICKLUNG,
                                   DISKONTFAKTOR
) {
    
    print("Run module: mod_ahv_postprocessing")
    
    # Berechnen Indikatoren und Zusammenfügen zum Finanzhaushalt -----------------
    AHV_FHH <- AHV_BILANZ %>%
        diskontierung(DISKONTFAKTOR) %>%
        left_join(mod_ahv_indices(
            AHV_FHH = .,
            RENTENENTWICKLUNG = RENTENENTWICKLUNG), by = "jahr")
    
    AHV_FHH_nom <- AHV_BILANZ %>%
        left_join(mod_ahv_indices(
            AHV_FHH = .,
            RENTENENTWICKLUNG = RENTENENTWICKLUNG), by = "jahr")
    
    # Output ---------------------------------------------------------------------
    if (PARAM_GLOBAL$flag_param_massn) {
        AHV_MASSNAHMEN_nom <- AHV_MASSNAHMEN
        AHV_MASSNAHMEN_real <- AHV_MASSNAHMEN %>% diskontierung(DISKONTFAKTOR)
        tl_out <- list(
            AHV_MASSNAHMEN_real = AHV_MASSNAHMEN_real,
            AHV_FHH = AHV_FHH,
            AHV_MASSNAHMEN_nom = AHV_MASSNAHMEN_nom,
            AHV_FHH_nom = AHV_FHH_nom
        )
    } else {
        tl_out <- list(
            AHV_FHH_nom = AHV_FHH_nom,
            AHV_FHH = AHV_FHH
        )
    }
    
    return(tl_out)
}
