---
title: "WHO STEPS NCD Risk Factor Survey — Fact Sheet"
output:
  html_document:
    theme: flatly
    self_contained: true
    css: null
  word_document: default
params:
  config: NULL
  output_dir: NULL
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE,
                      fig.width = 10, fig.height = 6, dpi = 150)
library(flextable); library(ggplot2); library(glue); library(patchwork)
library(stepssurvey)

cfg <- params$config

# Load pre-computed data from output_dir
data_dir <- file.path(params$output_dir, "data")
indicators     <- readRDS(file.path(data_dir, "indicators.rds"))
key_indicators <- readRDS(file.path(data_dir, "key_indicators.rds"))
plots          <- readRDS(file.path(data_dir, "plots.rds"))
```

<div style="background:#00427A; color:white; padding:15px 20px; margin-bottom:20px; border-radius:4px;">
<h1 style="margin:0; color:white;">`r cfg$country_name`</h1>
<h3 style="margin:5px 0 0; color:#B0D4F1;">WHO STEPS NCD Risk Factor Survey `r cfg$survey_year`</h3>
<p style="margin:5px 0 0; font-size:0.9em;">Population: ages `r cfg$age_min`–`r cfg$age_max` years</p>
</div>

## Key Risk Factor Prevalence

```{r overview-chart, fig.height=7}
plots$overview
```

## Summary Table

```{r key-table}
# Format threshold info
threshold_notes <- c()
if (!is.null(cfg$bp_sbp_threshold) && cfg$bp_sbp_threshold != 140) {
  threshold_notes <- c(threshold_notes,
    glue("BP threshold: ≥{cfg$bp_sbp_threshold}/{cfg$bp_dbp_threshold} mmHg"))
}
if (!is.null(cfg$bmi_overweight) && cfg$bmi_overweight != 25) {
  threshold_notes <- c(threshold_notes,
    glue("BMI overweight: ≥{cfg$bmi_overweight}"))
}
if (!is.null(cfg$glucose_threshold) && cfg$glucose_threshold != 7.0) {
  threshold_notes <- c(threshold_notes,
    glue("Glucose threshold: ≥{cfg$glucose_threshold} mmol/L"))
}
if (!is.null(cfg$chol_threshold) && cfg$chol_threshold != 5.0) {
  threshold_notes <- c(threshold_notes,
    glue("Cholesterol threshold: ≥{cfg$chol_threshold} mmol/L"))
}

key_indicators |>
  dplyr::mutate(
    Prevalence = paste0(round(estimate, 1), "% (",
                        round(lower, 1), "–", round(upper, 1), ")")
  ) |>
  dplyr::select(Domain = domain,
                `Risk Factor` = indicator,
                `Prevalence (95% CI)` = Prevalence) |>
  flextable() |>
  theme_vanilla() |>
  bold(part = "header") |>
  bg(part = "header", bg = "#00427A") |>
  color(part = "header", color = "white") |>
  merge_v(j = "Domain") |>
  valign(j = "Domain", valign = "top") |>
  autofit() |>
  set_caption(if (length(threshold_notes) > 0) {
    paste("Note: Non-default thresholds used:", paste(threshold_notes, collapse = "; "))
  } else {
    "WHO standard thresholds applied"
  })
```

## Risk Factor Profile

```{r radar-chart, fig.height=6}
if (!is.null(plots$radar)) {
  plots$radar
} else {
  build_radar_plot(key_indicators, cfg$country_name, cfg$survey_year)
}
```

## Prevalence by Sex

```{r sex-dashboard, fig.height=8}
if (!is.null(plots$sex_dashboard)) plots$sex_dashboard
```

## Forest Plot — Estimates with 95% Confidence Intervals

```{r forest-plot, fig.height=6}
if (!is.null(plots$forest)) {
  plots$forest
} else {
  build_forest_plot(key_indicators, cfg$country_name, cfg$survey_year)
}
```

---

<div style="font-size:0.85em; color:#666; border-top:1px solid #ccc; padding-top:10px;">
**Data source:** WHO STEPS NCD Risk Factor Survey, `r cfg$country_name` `r cfg$survey_year`<br>
**Age group:** `r cfg$age_min`–`r cfg$age_max` years<br>
**Analysis:** Generated by the `stepssurvey` R package using WHO-aligned methodology<br>
**Generated:** `r format(Sys.Date(), "%d %B %Y")`
</div>
