## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE)
library(mudnester)

## ----workflow-----------------------------------------------------------------
# Construct sample data
patient_data <- data.frame(
  patient_name = c("John Doe", "Jane Smith", "Alice Brown"),
  dob          = as.Date(c("1980-01-01", "1975-05-15", "1992-11-30")),
  mrn          = c("12345", "67890", "11111"),
  diagnosis    = c("Condition A", "Condition B", "Condition C"),
  severity     = c("mild", "moderate", "severe")
)

# Step 1: de-identify (typically done at data collection / storage time)
result <- suppressMessages(molting(patient_data))

# Step 2: share or archive result$deidentified
#         store result$lookup securely, separately

# Step 3: relink when authorised
relinked <- homing(
  deidentified_data = result$deidentified,
  lookup_table      = result$lookup
)

head(relinked)

## ----custom-hash--------------------------------------------------------------
result_custom <- suppressMessages(
  molting(patient_data, hash_col_name = "person_hash")
)

relinked_custom <- homing(
  result_custom$deidentified,
  result_custom$lookup,
  hash_col_name = "person_hash"
)

"patient_name" %in% names(relinked_custom)

## ----no-hash------------------------------------------------------------------
relinked_clean <- homing(
  result$deidentified,
  result$lookup,
  keep_hash = FALSE
)

names(relinked_clean)   # no row_hash column

## ----partial------------------------------------------------------------------
# Simulate a truncated lookup — only the first two rows
partial_lookup <- result$lookup[1:2, ]

relinked_partial <- homing(
  result$deidentified,
  partial_lookup
)

# Third row has NA identifiers
relinked_partial[, c("row_hash","patient_name","diagnosis")]

