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

## ----prerequisites------------------------------------------------------------
# for (pkg in c("DatabaseConnector", "Eunomia", "Andromeda")) {
#   if (!requireNamespace(pkg, quietly = TRUE)) install.packages(pkg)
# }
# 
# library(OdysseusCharacterizationModule)
# library(DatabaseConnector)
# library(Eunomia)

## ----connect------------------------------------------------------------------
# connectionDetails <- getEunomiaConnectionDetails()
# Eunomia::createCohorts(connectionDetails)
# connection <- connect(connectionDetails)

## ----common-params------------------------------------------------------------
# COHORT_ID  <- 1L          # Celecoxib new users
# CDM_SCHEMA <- "main"

## ----settings-basic-----------------------------------------------------------
# ocmSettings <- createOcmCovariateSettings(
#   analysisWindows = defineAnalysisWindows(
#     startDays = c(-365),
#     endDays   = c(-1)
#   ),
#   useBaseFeatures = list(
#     condition_occurrence = list(include = TRUE, type = "start"),
#     drug_exposure        = list(include = TRUE, atc = FALSE),
#     condition_era        = list(include = FALSE),
#     drug_era             = list(include = FALSE),
#     procedure_occurrence = list(include = FALSE),
#     observation          = list(include = FALSE),
#     device_exposure      = list(include = FALSE),
#     visit_occurrence     = list(include = FALSE),
#     measurement          = list(include = FALSE)
#   )
# )
# 
# class(ocmSettings)
# #> [1] "covariateSettings"
# 
# attr(ocmSettings, "fun")
# #> [1] "getDbOcmCovariateData"

## ----standalone---------------------------------------------------------------
# covData <- getDbOcmCovariateData(
#   connection        = connection,
#   cdmDatabaseSchema = CDM_SCHEMA,
#   cohortTable       = "main.cohort",
#   cohortIds         = c(COHORT_ID),
#   rowIdField        = "subject_id",
#   covariateSettings = ocmSettings,
#   aggregated        = FALSE
# )

## ----covariates---------------------------------------------------------------
# covDf <- covData$covariates |> as.data.frame()
# cat("Total covariate entries:", nrow(covDf), "\n")
# cat("Unique patients:",        length(unique(covDf$rowId)), "\n")
# cat("Unique covariates:",      length(unique(covDf$covariateId)), "\n")
# head(covDf)

## ----covariate-ref------------------------------------------------------------
# refDf <- covData$covariateRef |> as.data.frame()
# head(refDf, 10)

## ----analysis-ref-------------------------------------------------------------
# analysisDf <- covData$analysisRef |> as.data.frame()
# analysisDf

## ----close-standalone---------------------------------------------------------
# Andromeda::close(covData)

## ----fe-integration-----------------------------------------------------------
# if (requireNamespace("FeatureExtraction", quietly = TRUE)) {
# 
#   covDataFE <- FeatureExtraction::getDbCovariateData(
#     connection            = connection,
#     cdmDatabaseSchema     = CDM_SCHEMA,
#     cohortDatabaseSchema  = CDM_SCHEMA,
#     cohortTable           = "cohort",
#     cohortIds             = c(COHORT_ID),
#     covariateSettings     = ocmSettings,
#     aggregated            = FALSE
#   )
# 
#   cat("Covariates (via FE):", nrow(as.data.frame(covDataFE$covariates)), "\n")
#   Andromeda::close(covDataFE)
# 
# } else {
#   message("FeatureExtraction not installed — skipping integration demo.")
# }

## ----combined-----------------------------------------------------------------
# if (requireNamespace("FeatureExtraction", quietly = TRUE)) {
# 
#   feSettings <- FeatureExtraction::createCovariateSettings(
#     useDemographicsGender    = TRUE,
#     useDemographicsAge       = TRUE,
#     useDemographicsIndexYear = TRUE
#   )
# 
#   combinedCovData <- FeatureExtraction::getDbCovariateData(
#     connection            = connection,
#     cdmDatabaseSchema     = CDM_SCHEMA,
#     cohortDatabaseSchema  = CDM_SCHEMA,
#     cohortTable           = "cohort",
#     cohortIds             = c(COHORT_ID),
#     covariateSettings     = list(feSettings, ocmSettings),
#     aggregated            = FALSE
#   )
# 
#   covDf <- as.data.frame(combinedCovData$covariates)
#   cat("Total covariate entries (combined):", nrow(covDf), "\n")
#   cat("Unique covariates (combined):",      length(unique(covDf$covariateId)), "\n")
# 
#   Andromeda::close(combinedCovData)
# 
# } else {
#   message("FeatureExtraction not installed — skipping combined demo.")
# }

## ----multi-domain-------------------------------------------------------------
# ocmSettingsWide <- createOcmCovariateSettings(
#   analysisWindows = defineAnalysisWindows(
#     startDays = c(-365, -30, 1),
#     endDays   = c(-1,   -1, 30)
#   ),
#   useBaseFeatures = list(
#     condition_occurrence = list(include = TRUE, type = "start"),
#     drug_exposure        = list(include = TRUE, atc = FALSE),
#     procedure_occurrence = list(include = TRUE),
#     condition_era        = list(include = FALSE),
#     drug_era             = list(include = FALSE),
#     observation          = list(include = FALSE),
#     device_exposure      = list(include = FALSE),
#     visit_occurrence     = list(include = TRUE, type = "start"),
#     measurement          = list(include = TRUE)
#   )
# )
# 
# covDataWide <- getDbOcmCovariateData(
#   connection        = connection,
#   cdmDatabaseSchema = CDM_SCHEMA,
#   cohortTable       = "main.cohort",
#   cohortIds         = c(COHORT_ID),
#   covariateSettings = ocmSettingsWide,
#   aggregated        = FALSE
# )
# 
# cat("Analyses:",   nrow(as.data.frame(covDataWide$analysisRef)), "\n")
# cat("Covariates:", nrow(as.data.frame(covDataWide$covariates)), "\n")
# 
# Andromeda::close(covDataWide)

## ----concept-set--------------------------------------------------------------
# ocmConceptSet <- createOcmCovariateSettings(
#   analysisWindows = defineAnalysisWindows(
#     startDays = c(-365),
#     endDays   = c(-1)
#   ),
#   useBaseFeatures = list(
#     condition_occurrence = list(include = FALSE),
#     condition_era        = list(include = FALSE),
#     drug_exposure        = list(include = FALSE),
#     drug_era             = list(include = FALSE),
#     procedure_occurrence = list(include = FALSE),
#     observation          = list(include = FALSE),
#     device_exposure      = list(include = FALSE),
#     visit_occurrence     = list(include = FALSE),
#     measurement          = list(include = FALSE)
#   ),
#   useConceptSetFeatures = list(
#     include = TRUE,
#     type    = "binary",
#     conceptSets = list(
#       hypertension = list(
#         items = list(
#           list(
#             concept            = list(CONCEPT_ID = 316866L),
#             includeDescendants = TRUE,
#             includeMapped      = FALSE,
#             isExcluded         = FALSE
#           )
#         ),
#         tables = c("condition_occurrence")
#       )
#     )
#   )
# )
# 
# covDataCS <- getDbOcmCovariateData(
#   connection        = connection,
#   cdmDatabaseSchema = CDM_SCHEMA,
#   cohortTable       = "main.cohort",
#   cohortIds         = c(COHORT_ID),
#   covariateSettings = ocmConceptSet,
#   aggregated        = FALSE
# )
# 
# cat("Concept-set covariates:", nrow(as.data.frame(covDataCS$covariates)), "\n")
# as.data.frame(covDataCS$covariateRef)
# 
# Andromeda::close(covDataCS)

## ----disconnect---------------------------------------------------------------
# disconnect(connection)

