context("General functions")


test_that("General samples-data retrievals work using WQP tests", {
  testthat::skip_on_cran()
  nameToUse <- "pH"
  pHData <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-04024315",
    characteristic = nameToUse
  )
  expect_is(pHData$Activity_StartDateTime, "POSIXct")
  expect_type(pHData$USGSpcode, "character")
  expect_type(pHData$Result_Measure, "double")
  expect_type(pHData$SampleCollectionMethod_Identifier, "character")

  # testing lists:
  startDate <- as.Date("2022-01-01")
  secchi_ops <- check_waterdata_sample_params(
    "observedproperty",
    text = "secchi"
  )

  state_fips <- paste0("US:", stateCdLookup("WI", "id"))
  lakeData <- read_waterdata_samples(
    activityStartDateLower = startDate,
    activityStartDateUpper = "2024-01-01",
    stateFips = "US:55",
    characteristicUserSupplied = secchi_ops$observedProperty,
    dataProfile = "narrow"
  )

  expect_true(nrow(lakeData) > 0)

  lakeSites <- read_waterdata_samples(
    monitoringLocationIdentifier = unique(lakeData$Location_Identifier),
    dataType = "locations",
    dataProfile = "site"
  )
  expect_type(lakeSites, "list")

  rawPcode <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-01594440",
    usgsPCode = "01075"
  )

  expect_true(all(
    c("url", "queryTime", "headerInfo") %in%
      names(attributes(rawPcode))
  ))

  pHData <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-04024315",
    characteristic = "pH",
    dataProfile = "narrow"
  )

  expect_true(all(
    c("url", "queryTime", "headerInfo") %in%
      names(attributes(pHData))
  ))
})


context("samples-data samples")
test_that("samples-data activities working", {
  testthat::skip_on_cran()

  activityInfo <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-01594440",
    dataType = "activities"
  )
  expect_true(nrow(activityInfo) > 0)
})

context("samples-data project")
test_that("samples-data project working", {
  testthat::skip_on_cran()
  type <- "Stream"
  county_fips <- countyCdLookup("WI", "Dane")
  projectInfo <- read_waterdata_samples(
    countyFips = county_fips,
    siteTypeName = type,
    dataType = "projects"
  )
  expect_true(ncol(projectInfo) >= 0)
})

context("summary_waterdata_samples")
test_that("summary_waterdata_samples working", {
  testthat::skip_on_cran()
  testthat::skip_on_ci()

  site1 <- summarize_waterdata_samples(
    monitoringLocationIdentifier = "USGS-01594440"
  )
  expect_is(site1, "data.frame")
})

test_that("profiles", {
  testthat::skip_on_cran()
  testthat::skip_on_ci()
  # Data profiles: "Organization Data"
  org_data <- read_waterdata_samples(
    countyFips = countyCdLookup("WI", "Dane"),
    dataType = "organizations"
  )

  # Data profiles: "Site Data Only"
  site_data <- read_waterdata_samples(
    countyFips = countyCdLookup("WI", "Dane"),
    dataType = "locations"
  )

  expect_true(all(
    c("ProviderName", "Location_Identifier") %in% names(site_data)
  ))

  # Data profiles: "Project Data"
  project_data <- read_waterdata_samples(
    countyFips = countyCdLookup("WI", "Dane"),
    dataType = "projects"
  )

  expect_true(all(
    c(
      "Org_Identifier",
      "Org_FormalName"
    ) %in%
      names(project_data)
  ))

  # Data profiles: "Project Monitoring Location Weighting Data"
  proj_mlwd <- read_waterdata_samples(
    countyFips = countyCdLookup("WI", "Dane"),
    dataType = "projects",
    dataProfile = "projectmonitoringlocationweight"
  )

  expect_true(all(
    c(
      "Org_Identifier",
      "Org_FormalName"
    ) %in%
      names(proj_mlwd)
  ))

  # Data profiles: "Sample Results (biological metadata)"
  samp_bio <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-04024315",
    dataProfile = "basicbio",
    dataType = "results"
  )

  expect_true(all(
    c(
      "Org_Identifier",
      "Org_FormalName"
    ) %in%
      names(samp_bio)
  ))

  # Data profiles: "Sample Results (narrow)"
  samp_narrow <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-04024315",
    dataProfile = "narrow",
    dataType = "results"
  )

  expect_true(all(
    c(
      "Org_Identifier",
      "Org_FormalName"
    ) %in%
      names(samp_narrow)
  ))

  # Data profiles: "Sampling Activity"
  samp_activity <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-04024315",
    dataProfile = "sampact", # Sampling Activities
    dataType = "activities"
  )

  expect_true(all(
    c(
      "Org_Identifier",
      "Org_FormalName"
    ) %in%
      names(samp_activity)
  ))

  # Data profile: "Result Detection Quantitation Limit Data"
  dl_data <- read_waterdata_samples(
    monitoringLocationIdentifier = "USGS-04024315",
    dataType = "results",
    dataProfile = "resultdetectionquantitationlimit"
  )

  expect_true(all(
    c(
      "Org_Identifier",
      "Org_FormalName"
    ) %in%
      names(dl_data)
  ))
})
