#' @title Berechnet die Ausgaben des EO Betreuungsurlaubs nach Art. 16n-s EOG
#'
#' @description Berechnet die Ausgaben in der EO fuer die Entschaedigung fuer
#'   Eltern, die ein wegen Krankheit oder Unfall gesundheitlich schwer
#'   beeintraechtigtes Kind betreuen.
#'
#' @param PARAM_GLOBAL a single row data frame, containing global parameters of
#'   which we use the following parameter:
#'   - `jahr_abr`: year of abrechnung
#'
#' @param PARAM_GLOBAL a single row data frame, containing EO-specific parameters
#'   of which we use the following parameters:
#'   - `anteil_bsu`: Anteil Ausgaben BSU an EO Abrechnung Ausgaben total
#'   - `bsu1`: Korrekturfaktor Ausgaben BSU für das Jahr jahr_abr+1
#'   - `bsu2`: Korrekturfaktor Ausgaben BSU für das Jahr jahr_abr+2
#'   - `bsu3`: Korrekturfaktor Ausgaben BSU für das Jahr jahr_abr+3
#'   - `erhoehung_eo_tx` Erhöhung Beitragssatz (logical)
#'
#' @param EO_ABRECHNUNG data frame containing btr_vs_ag, see function
#' \code{\link{mod_input_eo_abrechnung}}.
#'
#' @param ALLE_BEITRAGSSAETZE a data frame containing the actual beitragssaetze for
#'   AHV, IV, EO and ALV
#'
#' @param EO_JOUR_MUTTER a data frame which contains the average daily rate of a
#' mother, beginning from jahr_abr till 2070
#'
#' @param EO_JOUR_MANN a data frame which contains the average daily rate of a
#' man, beginning from jahr_abr till 2070
#'
#' @param LEISTUNGSTAGE Dataframe, welches die Leistungstage gemäss Massnahme 
#' beinhaltet.
#'
#' @param TCR_EOMAX un dataframe, contenant la variation annuelle à appliquer à
#'  la somme total des APG, comme résultat de l'augmentation de l'allocation
#'  maximale journalière, see function \code{\link{mod_eo_delta_eomax}}
#'
#' @param list compulsory argument in all modules. Instead of feeding
#'   a module with tidy data frames, it can be fed by a `tidylist`, which
#'   contains the tidy data frames. All listed data frames must to be present in
#'   the `tidylist`, with the same name. Additionally, the `tidylist` can also
#'   contain data frames that are not used by the module.
#'
#' @return a `tidylist` containing the following tidy data frames:
#'  - `EO_AUSGABEN_BSU`
#'
#' @author [MAS BSV](mailto:sekretariat.mas@bsv.admin.ch)
#'
#' @export


mod_eo_bsu <- function(PARAM_GLOBAL,
                       EO_ABRECHNUNG,
                       ALLE_BEITRAGSSAETZE,
                       EO_JOUR_MUTTER,
                       EO_JOUR_MANN,
                       LEISTUNGSTAGE,
                       TCR_EOMAX,
                       list = NULL) {

  # FIXME: Berechnung ueberarbeiten

  mod_init()

  EO_JOUR_PAAR <- EO_JOUR_MUTTER %>%
    left_join(EO_JOUR_MANN, by = "jahr") %>%
    rowwise() %>%
    mutate(moyen = mean(c(moyen.x, moyen.y), na.rm = T)) %>%
    dplyr::select(jahr, moyen.m = moyen.y, moyen.f = moyen.x, moyen.paar = moyen)
  
  AUSGABEN_VOR <- LEISTUNGSTAGE %>%
    dplyr::select(jahr, schwer_erkrak_frau, schwer_erkrak_man, schwer_erkrak_paar) %>%
    left_join(EO_JOUR_PAAR, by = "jahr") %>%
    left_join(TCR_EOMAX, by = "jahr") %>%
    na.omit() %>%
    mutate(ausg_total = (moyen.paar * schwer_erkrak_paar +
                           moyen.m * schwer_erkrak_man +
                           moyen.f * schwer_erkrak_frau) * delta_eomax) %>%
    dplyr::select(jahr, ausg_total)

  AUSGABEN_BSU_ABR <- AUSGABEN_VOR %>%
    select(jahr, ausg_total) %>%
    filter(jahr == PARAM_GLOBAL$jahr_abr)


  AUSGABEN_TOT_ABR <- filter(
    EO_ABRECHNUNG, jahr == PARAM_GLOBAL$jahr_abr)[, "aus_tot"]$aus_tot
  ANTEIL_BSU <- PARAM_GLOBAL$anteil_bsu
  AUSGABEN_DECOMPTE <- AUSGABEN_TOT_ABR * ANTEIL_BSU

  factor_justierung <- AUSGABEN_DECOMPTE / AUSGABEN_BSU_ABR$ausg_total

  EO_AUSGABEN_BSU <- AUSGABEN_VOR %>%
    replace(is.na(.), 0) %>%
    arrange(jahr) %>%
    mutate(just_faktor = factor_justierung) %>%
    mutate(
      ausg_total_just = ausg_total * just_faktor,
    ) %>%
    dplyr::select(
      jahr,
      ausg_total_just,
      just_faktor
    )

  # FIXME: Manuelle Korrektur BSU fuer FHH Juni 2022

  EO_AUSGABEN_BSU <- EO_AUSGABEN_BSU %>%
    mutate(
      corr_factor = case_when(
        jahr > PARAM_GLOBAL$jahr_abr ~ PARAM_GLOBAL$bsu1,
        TRUE ~ 1L
      )
    ) %>%
    mutate(
      corr_factor2 = case_when(
        jahr > (PARAM_GLOBAL$jahr_abr + 1) ~ PARAM_GLOBAL$bsu2,
        TRUE ~ corr_factor
      )
    ) %>%
    mutate(
      corr_factor3 = case_when(
        jahr > (PARAM_GLOBAL$jahr_abr + 2) ~ PARAM_GLOBAL$bsu3,
        TRUE ~ corr_factor2
      )
    ) %>%
    mutate(ausg_total_just = ausg_total_just * corr_factor3) %>%
    dplyr::select(jahr, ausg_total_just)

  # End FIXME

  # Aenderung Ausgaben durch Beitragssatzaenderungen in den Sozialversicherungen

  if (isTRUE(PARAM_GLOBAL$erhoehung_eo_tx)) {
    AHV_IV_EO_TX_GO <- ALLE_BEITRAGSSAETZE %>%
      filter(jahr == PARAM_GLOBAL$jahr_abr) %>%
      dplyr::select(ahv_iv_eo_tx_go)

    EO_AUSGABEN_BSU <- ALLE_BEITRAGSSAETZE %>%
      left_join(EO_AUSGABEN_BSU, by = "jahr") %>%
      mutate(
        eo_var_tx = ifelse(jahr > PARAM_GLOBAL$jahr_abr,
          (ahv_iv_eo_tx_neu - AHV_IV_EO_TX_GO$ahv_iv_eo_tx_go) / 2 *
            ausg_total_just * 1 / (1 + (AHV_IV_EO_TX_GO$ahv_iv_eo_tx_go / 2)),
          0
        ),
        ausg_total_just = ausg_total_just + eo_var_tx
      )
  }

  mod_return(
    EO_AUSGABEN_BSU
  )
}
