#function 2c

#works but I have to run the unnest dataframes functions before
# basically combines the tw functions from before
download_all_jobs <- function(searchterm, location, radius) {
  library(httr)
  library(dplyr)
  library(jsonlite)
  
  # Authentication setup (assuming these are constants for all requests)
  headers <- c("Content-Type" = 'application/x-www-form-urlencoded')
  auth_data <- list(
    client_id = 'c003a37f-024f-462a-b36d-b001be4cd24a',
    client_secret = '32a39620-32b3-4307-9aa1-511e3d7f48a8',
    grant_type = 'client_credentials'
  )
  # Fetch access token
  res <- POST(url = 'https://rest.arbeitsagentur.de/oauth/gettoken_cc',
              add_headers(.headers = headers),
              body = auth_data,
              encode = 'form')
  token <- content(res)$access_token
  
  # API Request setup
  base_url <- "https://rest.arbeitsagentur.de/jobboerse/jobsuche-service/pc/v4/jobs"
  params <- list(
    "was" = searchterm,
    "wo" = location,
    size = 200,
    "umkreis" = radius
  )
  
  # Initial API Call to get total number of results
  initial_req <- GET(url = base_url, add_headers(OAuthAccessToken = token, Accept = "application/json"), query = params)
  initial_data <- jsonlite::fromJSON(rawToChar(initial_req$content))
  total_offers <- initial_data$maxErgebnisse
  total_pages <- ceiling(total_offers / 200)
  
  
  # Fetch all pages
  all_pages_data <- list()
  
  # Loop through each page and fetch data
  for (page in 1:total_pages) {
    params$page <- page
    response <- GET(url = base_url, add_headers(OAuthAccessToken = token, Accept = "application/json"), query = params)
    page_data <- jsonlite::fromJSON(rawToChar(response$content))
    all_pages_data[[page]] <- as.data.frame(page_data$stellenangebote)
  }
  
  # Combine all pages into one data frame
  job_postings <- bind_rows(all_pages_data)
  
  unnest_dataframes <- function(x) {
    y <- do.call(data.frame, x)
    if ("data.frame" %in% sapply(y, class)) {
      return(unnest_dataframes(y))  # Recursively call unnest_dataframes if any columns are data frames
    }
    return(y)  # Return the flattened data frame
  }
  
  # Unnest the data frame if needed
  job_postings_unnested <- unnest_dataframes(job_postings)
  
  return(job_postings_unnested)
}

result_df <- download_all_jobs("Daten", "Stuttgart", 100)
