## ----setup--------------------------------------------------------------------
#| include: false
library(tidyverse)
library(knitr)

vignette_dir <- dirname(knitr::current_input(dir = TRUE))
pkg_root <- normalizePath(file.path(vignette_dir, ".."), winslash = "/", mustWork = TRUE)

lookup_path <- file.path(pkg_root, "data-raw", "anztox", "endpoint_2016_to_2000_lookup.csv")

if (!file.exists(lookup_path)) {
  message("Note: Lookup CSV not found. Rendering with empty structure.")
  lookup_df <- tibble(
    endpoint_2016_raw = character(),
    endpoint_2016_abbrev = character(),
    endpoint_2016_norm = character(),
    n_rows_2016 = integer(),
    endpoint_2000_code = character(),
    map_method = character(),
    needs_review = logical()
  )
} else {
  lookup_df <- read_csv(lookup_path, show_col_types = FALSE)
}


## ----mapping-rules------------------------------------------------------------
#| echo: false
mapping_rules <- tribble(
  ~Code, ~Meaning, ~"Example regex target",
  "MORT", "Mortality / lethality", "mortalit, lethality, survival, lc[0-9]",
  "IMM", "Immobilisation", "immobili",
  "REP", "Reproduction", "reproduct, fecund, offspring, young per",
  "DVP", "Development", "develop, embryo, larval, hatch",
  "HAT", "Hatching", "hatch",
  "GRO", "Growth", "growth, weight, length, biomass, leaf area",
  "PSE", "Photosynthesis / fluorescence", "photosyn, fluores, chl, chlorophyll",
  "POP", "Population", "populat, abundan",
  "LUM", "Luminescence", "luminesc, biolumines",
  "ABD", "Avoidance / behaviour", "avoid, behaviour, movement",
  "FERTILISATION", "Fertilisation", "fertili",
  "14CO2 UPTAKE", "Carbon uptake", "co2, carbon uptake",
  "GLUCOSEUTILISATION", "Glucose utilisation", "glucose",
  "PRP", "Prey capture / predation", "predat, prey",
  "PSR", "Physiological stress response", "stress, biomark"
)

kable(mapping_rules, format = "html")


## ----coverage-summary---------------------------------------------------------
#| echo: false
coverage <- lookup_df |>
  summarise(
    "Endpoint labels total" = n_distinct(endpoint_2016_raw),
    "Endpoint labels mapped" = sum(!is.na(endpoint_2000_code)),
    "Endpoint labels unmapped" = sum(is.na(endpoint_2000_code)),
    "Rows total (2016)" = sum(n_rows_2016, na.rm = TRUE),
    "Rows mapped" = sum(n_rows_2016[!is.na(endpoint_2000_code)], na.rm = TRUE),
    "Rows unmapped" = sum(n_rows_2016[is.na(endpoint_2000_code)], na.rm = TRUE)
  ) |>
  pivot_longer(everything(), names_to = "Metric", values_to = "Value")

kable(coverage, format = "html", col.names = c("Metric", "Value"))


## ----improvement-summary------------------------------------------------------
#| echo: false
# Hard-coded v1 baseline
v1_baseline <- tibble(
  Metric = c("Labels mapped", "Labels unmapped", "Rows mapped", "Rows unmapped"),
  v1 = c(149, 48, 2641, 153)
)

v2_current <- lookup_df |>
  summarise(
    "Labels mapped" = sum(!is.na(endpoint_2000_code)),
    "Labels unmapped" = sum(is.na(endpoint_2000_code)),
    "Rows mapped" = sum(n_rows_2016[!is.na(endpoint_2000_code)], na.rm = TRUE),
    "Rows unmapped" = sum(n_rows_2016[is.na(endpoint_2000_code)], na.rm = TRUE)
  ) |>
  pivot_longer(everything(), names_to = "Metric", values_to = "v2")

improvement <- v1_baseline |>
  left_join(v2_current, by = "Metric") |>
  mutate(
    Change = v2 - v1,
    Change = paste0(ifelse(Change > 0, "+", ""), Change)
  )

kable(improvement, format = "html", col.names = c("Metric", "v1", "v2", "Change"))


## ----unmapped-table-----------------------------------------------------------
#| echo: false
unmapped <- lookup_df |>
  filter(needs_review == TRUE | is.na(endpoint_2000_code)) |>
  select(endpoint_2016_raw, endpoint_2016_norm, n_rows_2016, needs_review) |>
  arrange(desc(n_rows_2016))

kable(unmapped, format = "html", col.names = c("Raw Label", "Normalised", "Row Count", "Needs Review"))

