---
title: "**`SHARK4R`** Bio-QC Report"
output: html_document
params:
  targetDataset: NULL
  depth_margin: 0
  depth_col: "water_depth_m"
  land_buffer: 0
  field: "platform_code"
  code_type: "SHIPC"
  only_bad:  FALSE
  only_bad_distance: FALSE
  threshold_col: "P99" # 99th percentile
  parameter: NULL
  direction: "above"
  threshold_group: "Parameter" # or "Sea basin" or "Scientific name"
  
---

## Check data

This is an R Markdown document that checks data that has passed initial delivery QC and is now in the test phase.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

## Load library and data

```{r loadlibdata, echo=FALSE, include = FALSE}
scriptstart = Sys.time()
library(SHARK4R)
library(DT)
library(skimr)
library(dplyr)
library(htmltools)
```

## Load dataset

```{r dataset, echo=FALSE, include=FALSE}
# params$targetDataset may be a zip file (uploaded) or a folder (downloaded)
if (file.exists(params$targetDataset)) {
  shark_data <- read_shark(params$targetDataset)
} else if (file.exists(params$targetDataset) && grepl("\\.zip$", params$targetDataset)) {
  # uploaded ZIP file
  shark_data <- read_shark(params$targetDataset)
} else {
  stop("Target dataset not found or invalid: ", params$targetDataset)
}

# Drop empty columns
shark_data <- shark_data %>% select(where(~!all(is.na(.))))
```

## Name of dataset checked

```{r datasetname, echo=FALSE}
cat("Processing dataset:", unique(shark_data$dataset_name), sep=" ")

# Identify datatype and parameters in the dataset
datatype <- unique(shark_data$delivery_datatype)
parameters <- unique(shark_data$parameter)

# Translate datatype names
datatype_internal <- translate_shark_datatype(datatype)
```

## Check fields

Uses `check_fields()` function to control if all required fields are present.

```{r checkfields, echo=FALSE}
safe_datatable(check_fields(shark_data,
                            datatype_internal))
```

## Overview of data

```{r overview, echo=FALSE}
skim(shark_data)
```

## View full dataset in table format

```{r viewtable, echo=FALSE}
safe_datatable(shark_data)
```

## Check station name and geographical bounding box

Uses `check_stations()` to control if station name is found in the SMHI curated list and location is within preset distance limit

```{r checkstations, echo = FALSE}
safe_datatable(match_station(shark_data$station_name))

safe_datatable(check_nominal_station(shark_data))

check_station_distance(shark_data,
                       plot_leaflet = TRUE,
                       only_bad = params$only_bad_distance)
```

## Check onland

Uses `check_onland()` to control if any locations are on land.

```{r checkonland, echo=FALSE}
cat("Buffer distance used:", params$land_buffer, "\n")

check_onland(shark_data,
             buffer = params$land_buffer,
             plot_leaflet = TRUE,
             only_bad = params$only_bad)
```

## Check depth

Uses `check_depth()` to control if any sample depths are incorrect.

```{r checkdepth, echo=FALSE}
cat("Depth margin used:", params$depth_margin, "\n")
cat("Depth column used:", params$depth_col, "\n")

safe_datatable(check_depth(shark_data,
                           depthmargin = params$depth_margin))
```

## Check outliers

Uses `check_outliers()` function to control if all values are within range.

```{r checkoutliers, echo=FALSE, results='asis'}
# Map param choices to specific RDS files
rds_file <- switch(params$threshold_group,
                   "Parameter" = "parameter.rds",
                   "Sea basin" = "sea_basin.rds",
                   "Scientific name" = "scientific_name.rds",
                   "parameter.rds"  # fallback (default)
)


# Map dropdown choices to specific RDS files
group_col <- switch(params$threshold_group,
                    "Parameter" = "parameter",
                    "Sea basin" = "location_sea_basin",
                    "Scientific name" = "scientific_name",
                    "parameter"  # fallback (default)
)

thresholds <- load_shark4r_stats(rds_file, verbose = FALSE)

all_tables <- tagList()

for (param in parameters) {
  data <- shark_data %>% filter(parameter == param)
  all_tables <- tagAppendChild(all_tables,
                               tagList(
                                 tags$h4(param),
                                 check_outliers(data, 
                                                param, 
                                                datatype, 
                                                params$threshold_col, 
                                                thresholds, 
                                                group_col, 
                                                params$direction)
                               )
  )
}

all_tables
```

## Check logical

Uses `check_parameter_rules()` function to control if data follows logical assumptions.

```{r checkrules, echo=FALSE}
safe_datatable(check_parameter_rules(shark_data))
```

```{r checkzeroes, echo=FALSE}
safe_datatable(check_zero_value(shark_data))
```

```{r checkzeropositions, echo=FALSE}
safe_datatable(check_zero_positions(shark_data))
```

```{r checklogical_a, echo=FALSE}
safe_datatable(check_value_logical(shark_data))
```

## Check codes

Uses `check_codes()` function to control if data follows the SMHI codelist.

```{r checkcode, echo=FALSE}
cat("Field used:", params$field, "\n")
cat("Code type used:", params$code_type, "\n")

safe_datatable(check_codes(shark_data, field = params$field, code_type = params$code_type))
```

## Match taxa

Uses `is_in_dyntaxa()` to find and match taxa names provided in the data against the Dyntaxa database. In addition to the Dyntaxa match it is recommended to run the `match_worms_taxa()` function to match against the WoRMS database.

```{r matchtaxa, echo=FALSE}
worms_match <- match_worms_taxa(shark_data$scientific_name, 
                                marine_only = FALSE,
                                verbose = FALSE)

if ("scientific_name" %in% colnames(shark_data)) {
  tagList(
    tags$h4("Matched taxa (Dyntaxa)"),
    safe_datatable(is_in_dyntaxa(shark_data$scientific_name,
                                 use_dwca = TRUE,
                                 return_df = TRUE)),
    tags$h4("Matched taxa (WoRMS)"),
    safe_datatable(select(worms_match, 
                          scientificname, 
                          lsid, 
                          match_type, 
                          valid_AphiaID))
  )
}
```

## Reproducibility

```{r reproducibility, echo=FALSE, include=TRUE}
# Date time
Sys.time()
# Here we store the session info for this script
sessioninfo::session_info()
```

## Runtime of script

```{r runtime, echo = FALSE, include=TRUE}
scriptend = Sys.time()
print(scriptend-scriptstart)
```
