---
title: "`r paste('Report for', params$clinic_name)`"
author: "Rian DeFaccio"
date: "`r Sys.Date()`"
format: 
  html:
    toc: true
    toc-location: left
    embed-resources: true
params:
  clinic_name: "Starlight Medical Center"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{r include=FALSE}
library(tidyverse)
```


```{r}
library(tidyverse)

clinics <- read.csv('../data/healthcare2/clinic.csv')
visits <- read.csv('../data/healthcare2/OutpatientVisit.csv')
```

```{r}
visits <- left_join(visits, clinics, by = 'ClinicCode')

v_num_patients <- visits %>% 
  filter(ClinicName == params$clinic_name) %>% 
  pull(PatientID) %>% 
  n_distinct()

v_num_visits <- visits %>% 
  filter(ClinicName == params$clinic_name) %>% 
  nrow()
```

This report highlights information for `r params$clinic_name`.

## Visits and Patients

There were `r v_num_patients %>% format(big.mark=',')` patients seen at the hospital this year, for a total of `r v_num_visits %>% format(big.mark=',')` visits.

Here is how this clinic stacked up against its peers.

```{r}
num_visits <- visits |>
  group_by(ClinicName) |>
  count()

median_visits <- median(num_visits$n)
current_visits <- visits |> filter(ClinicName == params$clinic_name) |> nrow()
diff <- current_visits - median_visits

if (diff > 0) {
  subtitle <- paste(
    'Had', 
    diff %>% round(0) %>% format(nsmall = 0, big.mark = ","), 
    'More Visits Than the Median Hospital'
  )
} else if (diff < 0) {
  subtitle <- paste(
    'Had', 
    diff %>% abs() %>% round(0) %>% format(nsmall = 0, big.mark = ","), 
    'Fewer Visits Than the Median Hospital'
  )
} else if (diff == 0){
  subtitle <- 'Had an Average Number of Visits'
}

current_clinic <- params$clinic_name

if (current_visits > 0) {
  ggplot(num_visits, 
         aes(x = reorder(ClinicName, -n),
             y = n)) +
    geom_col() +
    theme_minimal() +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major.x = element_blank(),
          axis.text.x = element_text(hjust = 0)) +
    ylab('Number of Visits') + xlab('') + 
    ggtitle(current_clinic,
            subtitle = subtitle) +
    geom_col(data = num_visits |> filter(ClinicName == params$clinic_name),
             aes(x = reorder(ClinicName, -n),
             y = n),
             fill = 'red') +
    scale_x_discrete(breaks = c()) +
    scale_y_continuous(label = scales::comma)
} else {
  print("This hospital had no visits.")
}
```


## Peak into Visits Data

Here is a sneak peak into the visits data...

```{r}
if (v_num_visits > 0) {
  visits %>% 
    filter(ClinicName == current_clinic) %>%
    select(PatientID, StaffID, VisitDate, ICD10_1, ICD10_2, ICD10_3) %>%
    head(6) %>% 
    knitr::kable() 
} else {
    print("There were no visits at this hospital.")
}
```


```{r}
current_clinic_type <- clinics %>%
  filter(ClinicName == current_clinic) %>%
  pull(ClinicDescription)
```

`r if (current_clinic_type == "Primary Care") "::: {.content-hidden}"`

## Emergency Room Details

Since this hospital is an ER, we would put information about ER visits here.

`r if (current_clinic_type == "Primary Care") ":::"`

`r if (current_clinic_type == "Emergency Department") "::: {.content-hidden}"`

## Primary Care Details

Since this hospital is a primary care clinic, we would put information about primary care visits here.

`r if (current_clinic_type == "Emergency Department") ":::"`













