---
title: "Clinical Prediction Model Report (TRIPOD+AI)"
format:
  html:
    default: true
  docx:
    default: false
params:
  bundle_path: NULL
---

```{r}
#| echo: false
#| message: false
bundle <- readRDS(params$bundle_path)
model <- bundle$model
validation <- bundle$validation
review <- bundle$review
sensitivity <- bundle$sensitivity
recommendation <- bundle$recommendation
```

## Model Summary

- **Engine:** `r model$engine`
- **Outcome:** `r model$outcome`
- **Predictors:** `r paste(model$predictors, collapse = ", ")`
- **Training sample size:** `r nrow(model$training_data)`

## Method Recommendation (AI-assisted)

```{r}
#| echo: false
if (!is.null(recommendation)) {
  cat(recommendation)
} else {
  cat("Not run.")
}
```

## Validation Performance

```{r}
#| echo: false
if (!is.null(validation)) {
  knitr::kable(validation$metrics)
} else {
  cat("Not run.")
}
```

### Confusion Matrix

```{r}
#| echo: false
if (!is.null(validation)) {
  knitr::kable(as.data.frame(validation$confusion_matrix$table))
} else {
  cat("Not run.")
}
```

*Validated on: `r if (!is.null(validation)) validation$validated_on else "N/A"`*

## Sensitivity Analysis

```{r}
#| echo: false
if (!is.null(sensitivity)) {
  knitr::kable(tidyr::pivot_wider(sensitivity$comparison, names_from = .metric, values_from = .estimate))
} else {
  cat("Not run.")
}
```

## Pipeline Review Flags

```{r}
#| echo: false
if (!is.null(review) && nrow(review$flags) > 0) {
  knitr::kable(review$flags)
} else {
  cat("No issues flagged.")
}
```

```{r}
#| echo: false
if (!is.null(review) && !is.null(review$ai_summary)) {
  cat(review$ai_summary)
}
```

## TRIPOD+AI Compliance Notes

This report was generated using the `triageR` package. Please review the
following before submission:

- Confirm validation was performed on an independent dataset (see
  "Validated on" above — `"training_data"` indicates internal-only validation,
  which is NOT sufficient for TRIPOD+AI compliance).
- Confirm sample size and events-per-variable are adequate (see Pipeline
  Review Flags above).
- Report calibration plots and confidence intervals alongside these point
  estimates for full TRIPOD+AI compliance.
- This report is a drafting aid, not a substitute for the full TRIPOD+AI
  checklist, available at the EQUATOR Network.

*Report generated by triageR on `r Sys.time()`.*
