## ----setup, message=FALSE-----------------------------------------------------
# Use development build when interactive *and* explicitly enabled via env var.
dev_mode <- (Sys.getenv("DEV_VIGNETTES", "false") == "true")

if (dev_mode && requireNamespace("pkgload", quietly = TRUE)) {
  pkgload::load_all(
    export_all = FALSE,
    helpers = FALSE,
    attach_testthat = FALSE
  )
} else {
  # fall back to the installed package (the path CRAN, CI, and pkgdown take)
  library(stamp)
}
old_opts <- st_opts_get()
old_alias <- st_alias_get()

## -----------------------------------------------------------------------------
root <- tempfile("stamp-retention-")
st_init(root)

## -----------------------------------------------------------------------------
pA <- "A.qs"
pB <- "B.qs"
pC <- "C.qs"

xA1 <- data.frame(a = 1:3)
xA2 <- data.frame(a = 2:4)
xA3 <- data.frame(a = 3:5)

xB1 <- data.frame(b = letters[1:3])
xB2 <- data.frame(b = letters[2:4])

xC1 <- data.frame(c = 10:12)

# Keep retention OFF initially so we can create multiple versions
st_opts(retain_versions = Inf)

st_save(xA1, pA, code = function(z) z, alias = NULL)
st_save(xA2, pA, code = function(z) z, alias = NULL)
st_save(xA3, pA, code = function(z) z, alias = NULL)

st_save(xB1, pB, code = function(z) z, alias = NULL)
st_save(xB2, pB, code = function(z) z, alias = NULL)

st_save(xC1, pC, code = function(z) z, alias = NULL)

## -----------------------------------------------------------------------------
# Show version directories for each artifact (now stored per-artifact)
# Versions are stored in <artifact_folder>/versions/<version_id>/
info_a <- st_info(pA, alias = NULL)
artifact_dir_a <- fs::path_dir(info_a$sidecar$path)
versions_dir_a <- fs::path(artifact_dir_a, "versions")

if (fs::dir_exists(versions_dir_a)) {
  cat("A.qs versions:\n")
  fs::dir_tree(versions_dir_a, recurse = 1)
}

print(st_versions(pA, alias = NULL))
print(st_versions(pB, alias = NULL))
print(st_versions(pC, alias = NULL))

## -----------------------------------------------------------------------------
# Dry run (safe preview)
st_prune_versions(path = pA, policy = 2, dry_run = TRUE, alias = NULL)

# Apply pruning
repA <- st_prune_versions(path = pA, policy = 2, dry_run = FALSE, alias = NULL)
repA
nrow(st_versions(pA, alias = NULL)) # <= 2; latest always protected

## -----------------------------------------------------------------------------
# Keep anything from the last 14 days; preview first
st_prune_versions(policy = list(days = 14), dry_run = TRUE, alias = NULL)

# Apply
repAll <- st_prune_versions(policy = list(days = 14), alias = NULL)
head(repAll)

## -----------------------------------------------------------------------------
# Keep last 2 versions OR any version created within 7 days
st_prune_versions(policy = list(n = 2, days = 7), alias = NULL)

## -----------------------------------------------------------------------------
# Keep only the latest 2 versions per artifact going forward
st_opts(retain_versions = 2)

# New save writes + immediate prune
xA4 <- data.frame(a = 4:6)
st_save(xA4, pA, code = function(z) z, alias = NULL)

nrow(st_versions(pA, alias = NULL)) # <= 2

## -----------------------------------------------------------------------------
st_opts(retain_versions = Inf) # keep all versions

## -----------------------------------------------------------------------------
pop <- data.frame(
  country = c("PER", "PER", "COL"),
  year = c(2023, 2024, 2023),
  reporting_level = c("national", "urban", "national"),
  pop = c(34e6, 12e6, 52e6)
)

p_pop <- "inputs/population.qs"

# Validates uniqueness by default and writes PK to sidecar
# Note: `st_save(..., pk = ...)` validates the keys against the provided data
# and persists the `pk` element into the artifact's sidecar (stmeta/).
st_save(pop, p_pop, pk = c("country", "year", "reporting_level"), alias = NULL)

## -----------------------------------------------------------------------------
st_inspect_pk(p_pop) # read PK from sidecar
# If an older artifact lacks PK or you need to repair metadata, use:
st_add_pk(p_pop, keys = c("country", "year", "reporting_level"))

## -----------------------------------------------------------------------------
obj <- st_load(p_pop, alias = NULL)
attr(obj, "stamp_pk") # keys attached on load

## -----------------------------------------------------------------------------
st_opts("require_pk_on_load", .get = TRUE) # default FALSE
st_opts("warn_missing_pk_on_load", .get = TRUE) # default TRUE

# CI: make PK presence a hard requirement
st_opts(require_pk_on_load = TRUE, warn_missing_pk_on_load = FALSE)

## -----------------------------------------------------------------------------
pop <- data.frame(
  country = c("PER", "MEX"),
  year = c(2023, 2022),
  pop = c(34.5, 126.7)
)
pop <- st_with_pk(pop, c("country", "year"))

gdp <- data.frame(
  country = c("PER", "MEX"),
  year = c(2023, 2022),
  gdp = c(0.27, 1.26)
)
gdp <- st_with_pk(gdp, c("country", "year"))

merged <- merge(pop, gdp, by = c("country", "year"))
attr(merged, "stamp_pk") <- list(keys = c("country", "year")) # preserve grain
merged

## -----------------------------------------------------------------------------
side <- st_read_sidecar(p_pop)
names(side)
side$pk

## ----cleanup, include=FALSE---------------------------------------------------
suppressMessages(do.call(st_opts, old_opts))
if (!is.null(old_alias)) {
  st_init(old_alias$root, state_dir = old_alias$state_dir, verbose = FALSE)
}
unlink(root, recursive = TRUE)

