#' @title Globale Default-Parameter setzen.
#'
#' @description: Setzt Defaults in PARAM_GLOBAL, falls benötigte Parameterwerte nicht
#' manuell gesetzt wurden.
#'
#' @param PARAM_GLOBAL
#'
#' @param ECKWERTE
#'
#' @param ESTV
#'
#' @param AHV_ABRECHNUNG_PROV
#'
#' @param AHV_ABRECHNUNG_DEF
#'
#' @param IK
#'
#' @param RR_AVS
#'
#' @param BEV_BESTAND
#'
#' @param IV_SCHULD_SCEN
#'
#' @return 
#'  `PARAM_GLOBAL`      
#'
#' @author [MAS BSV](mailto:sekretariat.mas@bsv.admin.ch)
#'
#' @export

mod_ahv_param_global <- function(PARAM_GLOBAL,
                                 ECKWERTE,
                                 ESTV,
                                 AHV_ABRECHNUNG_PROV,
                                 AHV_ABRECHNUNG_DEF,
                                 IK,
                                 RR_AVS,
                                 BEV_BESTAND,
                                 IV_SCHULD_SCEN) {
  
  print("Run module: mod_ahv_param_global")
    
  # Mehrwertsteuer-Szenario.
  if (!"id_estv" %in% names(PARAM_GLOBAL)) {
    
      unique_idestv <- ESTV %>% 
        filter(laufjahr == max(laufjahr)) %>%
        filter(version == max(version)) %>% 
        distinct(idestv) %>% 
        pull(idestv)
      
      # Prüfe Anzahl verschiedener idestv
      if (length(unique_idestv) != 1) {

          stop("Mehrere verschiedene 'idestv' im Dataframe ESTV mit dem höchsten Wert von 
               'laufjahr' und 'version' gefunden. In PARAM_GLOBAL mit Hilfe des 
               Parameters 'idestv' eine Version explizit auswählen.")
        
      } else {
        
          PARAM_GLOBAL$id_estv <- unique_idestv
      }
      
      rm(unique_idestv)
  }
    
  # Eckwerte.
  if (!"id_eckwerte" %in% names(PARAM_GLOBAL)) {
      
      unique_id <- ECKWERTE %>%
        filter(laufjahr == max(laufjahr)) %>%
        filter(version == max(version)) %>% 
        distinct(id) %>% 
        pull(id)

      if (length(unique_id) != 1) {
        
          stop("Mehrere verschiedene 'id' im Dataframe ECKWERTE mit dem höchsten Wert 
          von 'laufjahr' und 'version' gefunden. Code wird angehalten.")
        
      } else {
        
          PARAM_GLOBAL$id_eckwerte <- unique_id
      }
      
      rm(unique_id)
  }
    
  # IV-Schulden Rückzahlungsprojektion.
  if (!"ivschuld_scen" %in% names(PARAM_GLOBAL)) {
      
      unique_id <- IV_SCHULD_SCEN %>% 
        filter(laufjahr == max(laufjahr)) %>%
        filter(version == max(version)) %>% 
        distinct(scen) %>% 
        pull(scen)
      
      if (length(unique_id) != 1) {
        
          stop("Mehrere verschiedene 'scen' im Dataframe IV_SCHULD_SCEN mit dem höchsten 
               Wert von 'laufjahr' und 'version' gefunden. In PARAM_GLOBAL mit Hilfe des 
               Parameters 'ivschuld_scen' eine Version explizit auswählen.")
        
      } else {
        
          PARAM_GLOBAL$ivschuld_scen <- unique_id
          
      }

      rm(unique_id)
  }
    
  # FHH für das mittlere Szenario (Basisszenario) berechnen.
  if(!"szenario_fhh" %in% names(PARAM_GLOBAL)) 
    PARAM_GLOBAL$szenario_fhh <- "mittel"

  # Setze 'bev_scenario' auf 'B_00_2025', falls Szenario 'günstig'.
  if (PARAM_GLOBAL$szenario_fhh == "guenstig" &
      (!"bev_scenario" %in% names(PARAM_GLOBAL) || 
       !startsWith(PARAM_GLOBAL$bev_scenario, "B_00"))) {
    
    PARAM_GLOBAL$bev_scenario <- "B_00_2025"
    
    warning("PARAM_GLOBAL$bev_scenario wurde auf B_00_2025 gesetzt, da 
            PARAM_GLOBAL$szenario_fhh == 'guenstig'")
  }
    
  # Setze 'bev_scenario' auf 'C_00_2025', falls Szenario 'ungünstig' ist.
  if (PARAM_GLOBAL$szenario_fhh == "unguenstig" &
      (!"bev_scenario" %in% names(PARAM_GLOBAL) || 
       !startsWith(PARAM_GLOBAL$bev_scenario, "C_00"))) {
    
    PARAM_GLOBAL$bev_scenario <- "C_00_2025"
    
    warning("PARAM_GLOBAL$bev_scenario wurde auf C_00_2025 gesetzt, da 
            PARAM_GLOBAL$szenario_fhh == 'unguenstig'")
  }
  
  # Bevölkerungsszenario für Basisprojektion.
  if (!"bev_scenario" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$bev_scenario <- "A_00_2025"
    
  # Abrechnungsjahr (mit Unterscheidung nach provisorischer oder definitiver Abrechnung).
  if(!"jahr_abr" %in% names(PARAM_GLOBAL)) {
      if(PARAM_GLOBAL$abr_prov) {
        
          PARAM_GLOBAL$jahr_abr <- max(AHV_ABRECHNUNG_PROV$jahr)
          
      } else {
        
          PARAM_GLOBAL$jahr_abr <- max(AHV_ABRECHNUNG_DEF$jahr) 
      }
  }
    
  # Preisbasis.
  if(!"jahr_preisbasis" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_preisbasis <- PARAM_GLOBAL$jahr_abr
    
  # Startjahr der Resultate.
  if(!"jahr_beginn" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_beginn <- 1997
    
  # Referenz-Jahr des Bevölkerungsstands.
  if (!"jahr_bev" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_bev <- BEV_BESTAND %>% 
          filter(!is.na(bevendejahr)) %>% 
          summarize(max_jahr = max(jahr)) %>% 
          pull(max_jahr)
    
  # Ende des Projektionshorizonts.
  if(!"jahr_ende" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_ende <- 2075
    
  # Darstellungsoptionen.
  if(!"intern_output_feature_on" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$intern_output_feature_on <- TRUE
  
  # Reduzierter Output.  
  if(!"light_output" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$light_output <- FALSE
    
  # Referenz-Jahr des Rentenregisters für Extrapolationen.
  if(!"jahr_rr" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_rr <- max(RR_AVS$jahr)
    
  # Laufjahr.
  if(!"jahr_lj" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_lj <- PARAM_GLOBAL$jahr_abr+1
    
  # Justierung der Modellprojektionen.
  if(!"justierung" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$justierung <- 0       
    
  # Referenz-Jahr IK.
  if(!"jahr_ik" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_ik <- max(IK$jahr)
    
  # Filenamen für das AHV-FHH '.xlsx'.
  if(!"file_ahvoutput" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$file_ahvoutput <- "FH_AHV.xlsx"
    
  # Rundung von Outputs auf Millionenstelle.
  if(!"spaltenmio" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$spaltenmio <- 
        "aus_tot, btr_vs_ag, btr_mwst, btr_bund, btr_uebr, ein_total, erg_umlag, kap_etr, erg_betr, kap, kap_oiv"
    
  # Spaltenindizes für Outputs.
  if(!"spaltenidx" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$spaltenidx <- "indx_ausg_ls, indx_uml_mwst, indx_uml_ls, indx_bed_mwst, indx_bed_ls, indx_kap_ausg, indx_kap_oiv_ausg, indx_ersq" 
    
    
  # Spaltenindizes für Outputs (Wachstumsratenberechnung).
  if(!"cols_with_pc" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$cols_with_pc <- "aus_tot, btr_vs_ag, btr_mwst, btr_bund, btr_uebr, ein_total"
    
  # Projektionshorizont für Massnahmen.
  if(!"jahr_lastoutput_massn" %in% names(PARAM_GLOBAL)) 
      PARAM_GLOBAL$jahr_lastoutput_massn <- PARAM_GLOBAL$jahr_lastoutput
    
  # Maximales inklusives Lebensalter für den geschätzten Rentenzyklus. Die Grenze wurde 
  # anhand der Datenverfügbarkeit seit 1997 gewählt. Die Ergebnisse sind nicht sensitiv 
  # gegenüber leichten Variationen.
  if(!"rentenzyklus_max_alt" %in% names(PARAM_GLOBAL)) 
    PARAM_GLOBAL$rentenzyklus_max_alt <- 65 + PARAM_GLOBAL$jahr_lj - 2005
    
  return(PARAM_GLOBAL = PARAM_GLOBAL)    
  
}
