######################################################
# CALCULATE COST DIFFERENTIAL FOR TWO SETS OF INPUTS #
##########################################################################################

# Clear workspace and load necessary packages.
pkg <- c("tidyverse", "magrittr", "simputation", "readxl", "zoo", "tidymodels",
         "collapse")
sapply(pkg, library, char = TRUE)

# Suppress column type guessing messages by 'readr' functions.
options(readr.show_col_types = FALSE)

# Disable 'collapse' namespace to avoid code collisions.
set_collapse(mask = NULL)

# Load base parameters.
source("scripts/base_par.R")
source("scripts/aux_fun.R")

par1 <- par2 <- par
rm(par)

par2$reftab <- 
  tribble(
    ~ year, ~ sex,       ~ refage,
    2023,   "f",       64      ,
    
    2025,   "f",       64 + 1/4,
    2026,   "f",       64 + 2/4,
    2027,   "f",       64 + 3/4,
    2028,   "f",       64 + 4/4,
    
    2029,   "f",       65 + 3 /12,
    2030,   "f",       65 + 6 /12,
    
    2023,   "m",       65,
    
    2029,   "m",       65 + 3 /12,
    2030,   "m",       65 + 6 /12)

(comp <- 
  compare_projections(par1, par2))

# compare_projections <- 
#   function(par1, par2) {
#   
#   source("scripts/aux_fun.R", loc = TRUE)
#     
#   # Base run.
#   print("Base run")
#   par <- par1
#   source("scripts/prepare_inputs.R", loc = TRUE)
#   if (par1$cv)
#     source("scripts/cv_out.R", loc = TRUE)
#   source("scripts/proj_out.R", loc = TRUE)
#   
#   res_init <- 
#     select(rtab, year, exp_init = exp_p)
# 
#   # Modified run.
#   print("Modified run")
#   par <- par2
#   source("scripts/prepare_inputs.R", loc = TRUE)
#   if (par2$cv)
#     source("scripts/cv_out.R", loc = TRUE)
#   source("scripts/proj_out.R", loc = TRUE)
#   
#   res_modi <- 
#     select(rtab, year, exp_modi = exp_p)
#   
#   # Return comparison table ('par1' minus 'par2').
#   return(
#     left_join(res_init, res_modi, by = "year") %>% 
#       mutate(cost_diff = exp_init - exp_modi) %>% 
#       select(year, exp_init, exp_modi, cost_diff)
#   )
# }
