###############
### README ###
#############

# TRIES to set the file paths programmatically so that they don't need to be manually defined.
# If individual scripts throw r/w errors,the paths should be redefined either in this script,
# or directly in the affected function.
#
# The scripts /require/ the 9.0 version of the SHP data (https://doi.org/10.48573/swnc-bn46)
# and does _not_ check that the correct version is accessed. If the most recent beta
# version is downloaded instead, additional filtering may have to be implemented by the user.

####################
### Input paths ###
##################

# Attempt to look for the SHP SPSS data directory in lieu of having to spell out file paths
# This /should/ work as long as
# - (1) the SHP data originates from a recent SWISSUbase download
# - (2) the folder path containing the SPSS longfiles has been unzipped
# - (3) it is located somewhere below the root home directory
# - (4) it is the only folder that matches the regex

# SHP data directory
suppressWarnings(
    dir_shp_spss <-
        fs::dir_ls(
            path = fs::path_home(),
            type = "directory",
            regexp = "swissubase_932_9_0/data/Data_SPSS$",
            recurse = TRUE,
            fail = FALSE
        )
)

# Metadata (for canton)
dir_shp_meta <- paste0(dir_shp_spss, "/SHP-DATA-CNEF-SPSS")

# Files
paths_meta <- list.files(
    path = dir_shp_meta,
    pattern = "\\d{4}.*sav",
    full.names = TRUE
)

### Long data
# Directory
dir_shp_longdata <- paste0(dir_shp_spss, "/SHP-Data-Longfile-SPSS")

# Individual file
path_spss_p <- paste0(dir_shp_longdata, "/SHPLONG_P_USER.sav")


####################
### Output paths ##
##################

plot_output_dir <- "~/Downloads/pdjplots/"

pqtfilepath_full <- paste0(here::here(), "/shplong_combined_modified.parquet")

pqtfilepath_1423 <- paste0(here::here(), "/shp_14_23_combined_modified.parquet")

m1predictspath <- paste0(here::here(), "/m1_predicts.parquet")


### Dependency check

CheckDeps <- function() {
    # generated with renv::dependencies(path =
    # renv::dependencies(path = c("00_data_preparation.R",
    # "01_model_construction.R", "02_predictmodels.R", "03_plots.R"),
    # quiet = TRUE)  |>
    # dplyr::pull("Package")  |> unique()  |> dput()
    pkgdeps <- c(
        "devtools",
        "knitr",
        "rmarkdown",
        "arrow",
        "data.table",
        "dplyr",
        "forcats",
        "haven",
        "labelled",
        "sjlabelled",
        "stringr",
        "tibble",
        "tidyr",
        "tidyselect",
        "cachem",
        "cli",
        "DHARMa",
        "ggeffects",
        "glmmTMB",
        "here",
        "memoise",
        "modelsummary",
        "performance",
        "purrr",
        "htmltools",
        "htmlwidgets",
        "plotly",
        "readr"
    )

    depdiff <- setdiff(pkgdeps, rownames(installed.packages()))

    if (length(!depdiff == 0)) {
        cli::cli_alert_info(
            "The following CRAN packages are required but not installed: {depdiff}"
        )

        prompt <- askYesNo("Would you like to install them now?")

        if (prompt == TRUE) {
            install.packages(depdiff)
        } else {
            stop("User aborted")
        }
    } else {
        cli::cli_alert_info("All CRAN dependencies met")
    }

    if (
        !"crosstalk" %in% rownames(installed.packages()) ||
            !packageVersion("crosstalk") == "1.1.1.9000"
    ) {
        cli::cli_alert_info(
            "The plotting operations require installation of a feature branch of the crosstalk package."
        )

        prompt_ct <- askYesNo(
            "Would you like to install it now? This will overwrite any existing crosstalk installations"
        )

        if (prompt == TRUE) {
            devtools::install_github(
                "rstudio/crosstalk",
                ref = "joe/feature/filter-select-default"
            )
        } else {
            stop("User aborted")
        }
    }
}

CheckDeps()