validate_obs <- function(obs_data, valid_species_list) {
  # VALIDATE Spp
  valid_species <- tibble(
    Species = unique(valid_species_list),
    in_ref_table = TRUE
  )
  obs_data <- obs_data %>%
    left_join(valid_species, by = "Species") %>%
    mutate(spp_valid = !is.na(in_ref_table)) %>%
    select(-in_ref_table)

  # VALIDATE Count
  obs_data <- obs_data %>%
    mutate(count_valid = (
      as.numeric(Count) >= MIN_COUNT &
        as.numeric(Count) <= MAX_COUNT &
        !is.na(Species)
    ))

  # VALIDATE Behavior
  obs_data <- obs_data %>%
    mutate(primary_behavior_lower = str_to_lower(PrimaryBehavior)) %>%
    left_join(
      ref_behavior %>%
        mutate(behavior = str_to_lower(behavior)),
      by = c("primary_behavior_lower" = "behavior")
    ) %>%
    mutate(primary_behavior_valid = (
      !is.na(in_ref_table) |
        is.na(PrimaryBehavior) |
        PrimaryBehavior == ""
    )) %>%
    select(-c(in_ref_table, primary_behavior_lower))

  obs_data <- obs_data %>%
    mutate(secondary_behavior_lower = str_to_lower(SecondaryBehavior)) %>%
    left_join(
      ref_behavior %>%
        mutate(behavior = str_to_lower(behavior)),
      by = c("secondary_behavior_lower" = "behavior")
    ) %>%
    mutate(secondary_behavior_valid = (
      !is.na(in_ref_table) |
        is.na(SecondaryBehavior) |
        SecondaryBehavior == ""
    )) %>%
    select(-c(in_ref_table, secondary_behavior_lower))

  # VALIDATE Weather
  obs_data <- obs_data %>%
    left_join(ref_weather, by = c("Weather" = "weather")) %>%
    mutate(weather_valid = (
      !is.na(in_ref_table) |
        is.na(Weather) |
        Weather == ""
    )) %>%
    select(-in_ref_table)

  # ANY ERROR
  obs_data <- obs_data %>%
    rowwise() %>%
    mutate(
      invalid_columns = paste(
        c(
          if_else(!spp_valid, "Species", ""),
          if_else(!count_valid, "Count", ""),
          if_else(!primary_behavior_valid, "PrimaryBehavior", ""),
          if_else(!secondary_behavior_valid, "SecondaryBehavior", ""),
          if_else(!weather_valid, "Weather", "")
        ),
        collapse = " "
      ),
      invalid_columns = gsub(" +", " ", invalid_columns),
      invalid_columns = gsub(" $", "", invalid_columns),
      invalid_columns = gsub("^ ", "", invalid_columns),
      invalid_columns = case_when(
        invalid_columns == "" ~ NA_character_,
        invalid_columns == "NA" ~ NA_character_,
        TRUE ~ invalid_columns
      )
    ) %>%
    ungroup() %>%
    select(-c(
      spp_valid, count_valid,
      primary_behavior_valid, secondary_behavior_valid,
      weather_valid
    ))

  obs_data$invalid_columns
}

validate_transects <- function(transects_data) {
  # VALIDATE TransWidth
  transects_data <- transects_data %>%
    mutate(trans_width_valid = !is.na(TxWidth))

  # ANY ERROR
  transects_data <- transects_data %>%
    rowwise() %>%
    mutate(
      invalid_columns = paste(
        c(
          if_else(!trans_width_valid, "TxWidth", "")
      ),
      collapse = " "
      ),
      invalid_columns = gsub(" +", " ", invalid_columns),
      invalid_columns = gsub(" $", "", invalid_columns),
      invalid_columns = gsub("^ ", "", invalid_columns),
      invalid_columns = case_when(
        invalid_columns == "" ~ NA_character_,
        invalid_columns == "NA" ~ NA_character_,
        TRUE ~ invalid_columns
      )
    ) %>%
    ungroup() %>%
    select(-c(trans_width_valid))

  transects_data$invalid_columns
}

# vim: set ft=r fenc=utf-8 tw=80 ts=2 sw=2 sts=2:
