library(lubridate)


conditional_save_downloads <- function(subfolder, jobs, refid_active_jobs = NULL, searchterm) {
  # Ensure the directory exists if not create the directory, completly depends of what I insert for subfolder
  if (!dir.exists(subfolder)) {
    dir.create(subfolder, recursive = TRUE)
  }
  
  # Generate today's date and filename base
  today <- Sys.Date() #tells you todays date
  is_monday <- wday(today) == 2  # wday returns 2 for Monday if locale is English
  base_filename <- paste0(subfolder, "/", today, "_jobs_", searchterm) #names the file
  
  # Check if it's Monday or if the subfolder is empty
  if (is_monday || length(list.files(subfolder)) == 0) {
    # Save the full dataset
    write.csv(jobs, paste0(base_filename, "_full.csv"), row.names = FALSE)
  } else { #if it is not monday skip the creation of the full dataset
    # It's not Monday and subfolder is not empty
    if (!is.null(refid_active_jobs)) {
      # Identify new and old job postings
      new_jobs <- jobs %>% filter(!refnr %in% refid_active_jobs)
      old_jobs <- jobs %>% filter(refnr %in% refid_active_jobs) %>% select(refnr)
      
      # Save new job postings if any
      if (nrow(new_jobs) > 0) {
        write.csv(new_jobs, paste0(base_filename, " new.csv"), row.names = FALSE)
      }
      
      # Save old job postings if any
      if (nrow(old_jobs) > 0) {
        write.csv(old_jobs, paste0(base_filename, " old.csv"), row.names = FALSE)
      }
    }
  }
  
  return("Files saved as required based on the conditions.")
}



ref_ids_active = result_df$refnr
conditional_save_downloads("/Users/alexanderunger/Desktop/AIDAHO_IDS_THAS/01_data/raw", result_df, ref_ids_active, "data")






