---
title: "STEPS Survey Data Book"
subtitle: "`r params$config$country_name` | `r params$config$survey_year`"
output:
  word_document: default
  html_document:
    theme: flatly
    toc: true
    toc_float: true
params:
  config: NULL
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      ft.keepnext = FALSE, ft.split = TRUE)
library(flextable); library(ggplot2); library(glue)

cfg      <- params$config
data_dir <- cfg$data_dir

# Read pre-computed data (with safe fallbacks)
detailed_tables <- if (file.exists(file.path(data_dir, "detailed_tables.rds"))) {
  readRDS(file.path(data_dir, "detailed_tables.rds"))
} else {
  list()
}
computed_results <- if (file.exists(file.path(data_dir, "computed_results.rds"))) {
  readRDS(file.path(data_dir, "computed_results.rds"))
} else {
  list()
}
plots <- if (file.exists(file.path(data_dir, "plots.rds"))) {
  readRDS(file.path(data_dir, "plots.rds"))
} else {
  list()
}

# Helper: render all tables whose registry ID matches a set of prefixes
show_tables <- function(id_prefixes) {
  found <- FALSE
  for (id in names(detailed_tables)) {
    if (any(startsWith(id, id_prefixes))) {
      found <- TRUE
      cat("\n\n")
      flextable::flextable_to_rmd(detailed_tables[[id]])
      cat("\n\n")
    }
  }
  if (!found) {
    cat("\n\n*No data available for this section.*\n\n")
  }
}
```

# `r cfg$country_name` -- WHO STEPS NCD Risk Factor Survey `r cfg$survey_year`

**Age range:** `r cfg$age_min`--`r cfg$age_max` years
**Survey instrument:** WHO STEPwise Approach to NCD Risk Factor Surveillance (STEPS)

All estimates are survey-weighted with 95% confidence intervals. Tables follow the standard WHO STEPS 3-panel format: **Men | Women | Both Sexes**, stratified by age group.

---

# Step 1: Behavioural Risk Factors

## Tobacco Use

```{r tobacco, results='asis'}
show_tables(c("T_smoking", "T_smokeless", "T_tobacco", "T_secondhand"))
```

```{r tobacco-chart, fig.width=8, fig.height=5}
if (!is.null(plots$tobacco_by_sex)) plots$tobacco_by_sex
```

## Alcohol Consumption

```{r alcohol, results='asis'}
show_tables("A_")
```

## Diet

```{r diet, results='asis'}
show_tables("D_")
```

## Physical Activity

```{r pa, results='asis'}
show_tables("P_")
```

---

# Step 1.5: Health History

```{r health, results='asis'}
show_tables("H_")
```

---

# Step 2: Physical Measurements

## Blood Pressure

```{r bp, results='asis'}
show_tables(c("M_bp", "M_heart"))
```

```{r bp-chart, fig.width=10, fig.height=5}
if (!is.null(plots$bp_by_age)) plots$bp_by_age
```

## Overweight and Obesity

```{r anthro, results='asis'}
show_tables(c("M_height", "M_bmi", "M_waist", "M_hip"))
```

```{r obesity-chart, fig.width=10, fig.height=5}
if (!is.null(plots$obesity_by_age)) plots$obesity_by_age
```

---

# Step 3: Biochemical Measurements

## Blood Glucose

```{r glucose, results='asis'}
show_tables(c("B_glucose"))
```

## Cholesterol and Lipids

```{r chol, results='asis'}
show_tables(c("B_chol", "B_hdl", "B_triglycerides"))
```

---

# Cardiovascular Risk

```{r cvd-risk, results='asis'}
show_tables("R_")
```

---

# Combined Risk Factors

```{r combined, results='asis'}
show_tables("RF_")
```

---

*Data book generated: `r Sys.Date()`*
*Analysis: WHO STEPS R Pipeline (stepssurvey package)*
*Country: `r cfg$country_name` | Survey year: `r cfg$survey_year`*
