---
title: "`r basename(params$title)`"
date: "`r Sys.time()`"
date-format: "YYYY-MM-DDTHH:mm:ss zzz"
abstract: "`r params$title`"
template-partials:
  - title-block.html
params:
  result: NULL
  title: 'dev'
  tmpdir: '.'
format:
  html:
    embed-resources: true
    toc: true
    toc-depth: 2
    toc-expand: true
    smooth-scroll: true
    css: style.css
---

# Summary

```{r}
#| label: Setup
#| echo: false

file.path(params$tmpdir, "with_options.rds") |>
  readRDS() |>
  options()

format_approved <- function(x) {
  if (all(is.na(x$approved))) {
    x$approved <- NULL
    return(x)
  }

  x$approved = ifelse(x$approved == TRUE, "\u2705 Yes", "\u274C No")

  x
}

knit_print.data.frame <- function(x, ...) {
  names(x) <- gsub(
    # Use space instead of . or _ in columns names
    pattern = "\\.|_",
    replacement = " ",
    x = names(x)
  )

  names(x) <- gsub(
    # Convert all column names to title case
    pattern = "\\b([[:alpha:]])([[:alpha:]]+)",
    replacement = "\\U\\1\\L\\2",
    x = names(x),
    perl = TRUE
  )

  for (i in which(vapply(X = x, FUN = is.logical, FUN.VALUE = logical(1)))) {
    x[[i]] <- ifelse(x[[i]], "Yes", "No") # Yes/No instead of TRUE/FALSE
  }

  x |>
    knitr::kable(format = "html") |>
    kableExtra::kable_styling(
      bootstrap_options = "striped",
      full_width = TRUE
    ) |>
    knitr::knit_print()
}

registerS3method(
  "knit_print",
  "data.frame",
  knit_print.data.frame,
  envir = asNamespace("knitr")
)
```

```{r}
#| label: Status
#| include: false
result <- readRDS(params$result)
```

```{r}
#| label: Report status
#| echo: false
status <- result$status

status_txt <- status[c("error", "warning")]
status_txt <- status_txt[lapply(status_txt, length) > 0] |>
  lapply(\(x) paste("*", x, collapse = "\n")) |>
  unlist()

whirl:::quarto_callout(
  text = status_txt,
  title = status$message,
  type = switch(
    status$message,
    "error" = "important",
    "warning" = "warning",
    "success" = "tip"
  ),
  collapse = status$message != "success"
)
```

```{r}
#| label: Renv
#| echo: false
#| eval: !expr file.exists(file.path(params$tmpdir, "renv_status.rds"))
params$tmpdir |>
  file.path("renv_status.rds") |>
  readRDS() |>
  whirl:::knit_print_whirl_renv_status()
```

```{r}
#| label: Used files
#| child: !expr file.path(params$tmpdir, "files.qmd")
#| eval: !expr (!is.null(result$files))
```

# Script
::: {.script-container}

```{r}
#| child: !expr file.path(params$tmpdir, "dummy.md")
```

:::

# Session info

```{r}
#| label: Session info settings
#| include: false
knitr::opts_chunk$set(
  error = FALSE,
  warning = FALSE,
  message = FALSE,
  echo = FALSE
)
```

:::{.callout-note collapse=false appearance="minimal"}
## Platform

```{r}
#| label: Platform
result$session$platform
```

:::

```{r}
#| label: Python
#| child: !expr file.path(params$tmpdir, "python.qmd")
#| eval: !expr grepl("\\.py$", params$title)
```

:::{.callout-note collapse=false appearance="minimal"}
## R packages used directly

```{r}
#| label: R packages used directly
result$session$R |>
  dplyr::filter(directly_used) |>
  dplyr::select("package", "version", "approved", "source") |>
  format_approved()
```

:::

:::{.callout-note collapse=true appearance="minimal"}
## R packages used indirectly

```{r}
#| label: R packages used indirectly
result$session$R |>
  dplyr::filter(!directly_used) |>
  dplyr::select("package", "version", "source")
```

:::

:::{.callout-note collapse=true appearance="minimal"}
## Environment variables

```{r}
#| label: Environment
result$session$environment
```

:::

:::{.callout-note collapse=true appearance="minimal"}
## Options

```{r}
#| label: Options
result$session$options
```

:::
