## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(formr)

# So this vignette runs offline, API calls are replayed from pre-recorded
# responses (vcr cassettes shipped with the package). With a real server you
# would instead call formr_api_authenticate() with your own host/credentials.
.formr_vcr <- requireNamespace("vcr", quietly = TRUE) &&
  nzchar(system.file("extdata/vcr_cassettes", package = "formr"))

if (.formr_vcr) {
  vcr::vcr_configure(
    dir = system.file("extdata/vcr_cassettes", package = "formr"),
    filter_sensitive_data = list(
      "formr-client-id-redacted"     = "dummy_client_id",
      "formr-client-secret-redacted" = "dummy_client_secret",
      "formr-host-redacted"          = "api.localhost"
    )
  )
  vcr::use_cassette("formr_api_authenticate", {
    formr_api_authenticate(host = "http://api.localhost",
      client_id = "dummy_client_id", client_secret = "dummy_client_secret",
      verbose = FALSE)
  })
}

## ----eval = .formr_vcr--------------------------------------------------------
# List all surveys
vcr::use_cassette("formr_api_survey_structure_fetch", {
  all_surveys <- formr_api_surveys(verbose = FALSE)
})
all_surveys

## ----eval = .formr_vcr--------------------------------------------------------
# Get the survey items as a tibble
vcr::use_cassette("formr_api_survey_structure_items", {
  items <- formr_api_survey_structure("platzhalter")
})

# Check the first few items
head(items)

## ----eval = FALSE-------------------------------------------------------------
# # Not run: needs a live formr server.
# # Download the survey as an Excel file
# formr_api_survey_structure(
#   survey_name = "daily_diary_v1",
#   format = "xlsx",
#   file_path = "backup_daily_diary.xlsx"
# )

## ----eval = FALSE-------------------------------------------------------------
# # Not run: needs a live formr server.
# # Upload a local Excel file
# # The survey name on the server is derived from the filename
# formr_api_upload_survey(file_path = "surveys/my_new_survey.xlsx")

## ----eval = FALSE-------------------------------------------------------------
# # Not run: needs a live formr server.
# formr_api_upload_survey(
#   survey_name = "google_imported_survey",
#   google_sheet_url = "https://docs.google.com/spreadsheets/d/..."
# )

## ----eval = FALSE-------------------------------------------------------------
# # Not run: needs a live formr server.
# # Delete a survey (prompts for confirmation by default)
# formr_api_delete_survey("old_pilot_survey")
# 
# # Force delete without confirmation (for automated scripts)
# formr_api_delete_survey("old_pilot_survey", prompt = FALSE)

