---
title: "`r params$title`"
date: "`r Sys.Date()`"
output:
  pdf_document:
    toc: true
    toc_depth: 3
  html_document:
    toc: true
    toc_depth: 3
    css: styles.css
params: 
  title: null
  eda_recommendations: null 
  modelling_assumptions: null
  submodule_results: null
  img_dir: null
---

<!-- Helper function -->
```{r setup, include=FALSE}
knit_plot <- function(result, img_name, alt = NULL) {

	# 1) Prefer an on-disk file if one exists
  	candidate <- file.path(params$img_dir, img_name)
	if (file.exists(candidate)) {
		return (knitr::include_graphics(candidate))
	}

	# 2) Fall back to a base64-encoded PNG in result$plot
	if (is.null(result$plot)) {
    	return (invisible(NULL))
  	}

  	b64 <- result$plot

  	# Strip data URI prefix
  	if (grepl("^data:image/[^;]+;base64,", b64, perl = TRUE)) {
    	b64 <- sub("^data:image/[^;]+;base64,", "", b64, perl = TRUE)
  	}

  	# Decode and write to a temporary file
  	b64_decoded <- base64enc::base64decode(b64)
	output_path <- tempfile()
    writeBin(b64_decoded, output_path, useBytes = TRUE)
	knitr::include_graphics(output_path)

}

```

<!-- Render the EDA Summary -->

```{r, echo = FALSE}
if (!is.null(params$eda_recommendations)) {
	output <- knitr::knit_child("eda_summary.Rmd", envir = environment())
} else {
	output <- character()
}

knitr::asis_output(output)
```

<!-- Render the EDA Submodules -->

```{r, echo = FALSE}
outputs <- character()

for (entry in params$submodule_results) {

	# Ignore FFA submodules
	if (entry$name != "Change Point Detection" && entry$name != "Trend Detection") {
		next
	}

	# Add the header for the submodule
	outputs <- c(outputs, glue("\n\n## {entry$name} ({entry$start}-{entry$end})\n\n"))

	# Create a list with the start year, end year, and image directory
	additional_params <- list(
		start = entry$start,
		end = entry$end,
		img_dir = img_dir
	)

	# Add the sub-report for each item in the entry
	for (item in names(entry$tests)) {
		env <- new.env()
		env$params <- c(entry$tests[[item]], additional_params)

		child <- knitr::knit_child(glue("{item}_report.Rmd"), envir = env)
		outputs <- c(outputs, child)
	}

} 

knitr::asis_output(outputs)
```

<!-- Render the FFA Summary -->

```{r, echo = FALSE}
if (!is.null(params$modelling_assumptions)) {
	output <- knitr::knit_child("ffa_summary.Rmd", envir = environment())
} else {
	output <- character()
}

knitr::asis_output(output)
```

<!-- Render the FFA Submodules -->

```{r, echo = FALSE}
outputs <- character()

for (entry in params$submodule_results) {

	# Ignore EDA submodules
	if (entry$name == "Change Point Detection" || entry$name == "Trend Detection") {
		next
	}

	# Create a list with the start year, end year, and image directory
	additional_params <- list(
		start = entry$start,
		end = entry$end,
		img_dir = img_dir
	)

	# Add the sub-report for the entry
	env <- new.env()

	if (entry$name == "Distribution Selection") {
		item <- "selection"
	} else if (entry$name == "Parameter Estimation") {
		item <- "estimation"
	} else if (entry$name == "Uncertainty Quantification") {
		item <- "uncertainty"
	} else if (entry$name == "Model Assessment") {
		item <- "assessment"
	}

	env$params <- c(entry[[item]], additional_params)
	child <- knitr::knit_child(glue("{item}_report.Rmd"), envir = env)
	outputs <- c(outputs, child)
} 

knitr::asis_output(outputs)
```


