
test_that("errorProfiles works in default settings", {
  skip_on_cran()
  skip_on_os("mac", arch = "aarch64")
  skip_if_not_installed("randomForest")
  skip_if_not_installed("scam")
  data(splotdata)
  splotdata <- sf::st_drop_geometry(splotdata)
  predictors <- terra::rast(system.file("extdata","predictors_chile.tif", package="CAST"))

  set.seed(100)
  model <- caret::train(splotdata[,6:16], splotdata$Species_richness, ntree = 10,
                        trControl = caret::trainControl(method = "cv", savePredictions = TRUE))

  AOA <- CAST::aoa(predictors, model,verbose=F)

  # DI ~ error
  errormodel_DI <- CAST::errorProfiles(model, AOA, variable = "DI")

  expected_error_DI = terra::predict(AOA$DI, errormodel_DI)



  #test model fit:
  expect_equal(round(as.numeric(summary(errormodel_DI$fitted.values)),2),
               c(14.25, 14.34, 15.21, 17.23, 18.70, 27.46))
  # test model predictions

  s <- as.vector(summary(terra::values(expected_error_DI)))
  s <- as.numeric(sub(".*:\\s*", "", s))
  expect_equal(s,c(14.26,27.46,27.46,26.81,27.46,27.47,17678.00))
})


test_that("errorProfiles works in with LPD", {
  skip_on_cran()
  skip_on_os("mac", arch = "aarch64")
  skip_if_not_installed("randomForest")
  skip_if_not_installed("scam")
  data(splotdata)
  splotdata <- sf::st_drop_geometry(splotdata)
  predictors <- terra::rast(system.file("extdata","predictors_chile.tif", package="CAST"))

  set.seed(100)
  model <- caret::train(splotdata[,6:16], splotdata$Species_richness, ntree = 10,
                        trControl = caret::trainControl(method = "cv", savePredictions = TRUE))

  AOA <- CAST::aoa(predictors, model, LPD = TRUE, maxLPD = 1,verbose=F)
  errormodel_LPD <- CAST::errorProfiles(model, AOA, variable = "LPD")
  expected_error_LPD = terra::predict(AOA$LPD, errormodel_LPD)


  #test model fit:
  expect_equal(round(as.numeric(summary(errormodel_LPD$fitted.values)),2),
               c(16.36, 16.36, 16.36, 16.36, 16.36, 16.36))
  # test model predictions

  s <- as.vector(summary(terra::values(expected_error_LPD)))
  s <- as.numeric(sub(".*:\\s*", "", s))
  expect_equal(s,c(16.36,16.36,16.36,16.36,16.36,16.36,17678.00))

})



test_that("errorProfiles works for multiCV", {
  skip_on_cran()
  skip_on_os("mac", arch = "aarch64")
  skip_if_not_installed("randomForest")
  skip_if_not_installed("scam")
  data(splotdata)
  splotdata <- sf::st_drop_geometry(splotdata)
  predictors <- terra::rast(system.file("extdata","predictors_chile.tif", package="CAST"))

  set.seed(100)
  model <- caret::train(splotdata[,6:16], splotdata$Species_richness, ntree = 10,
                        trControl = caret::trainControl(method = "cv", savePredictions = TRUE))

  AOA <- CAST::aoa(predictors, model,verbose=F)
  set.seed(100)
  errormodel_DI = suppressWarnings(errorProfiles(model, AOA, multiCV = TRUE, length.out = 3))
  expected_error_DI = terra::predict(AOA$DI, errormodel_DI)

  #test model fit:
  expect_equal(round(as.numeric(summary(errormodel_DI$fitted.values)),2),
               c(12.53, 17.21, 26.80, 26.19, 35.28, 35.30))
  # test model predictions

  s <- as.vector(summary(terra::values(expected_error_DI)))
  s <- as.numeric(sub(".*:\\s*", "", s))
  expect_equal(s,c(13.11,32.58,35.05,32.54,35.30,35.30,17678.00))
})

test_that("plot for errorModel runs and returns ggplot", {
  skip_on_cran()
  skip_on_os("mac", arch = "aarch64")
  skip_if_not_installed("randomForest")
  skip_if_not_installed("scam")
  data(splotdata)
  splotdata <- sf::st_drop_geometry(splotdata)
  predictors <- terra::rast(system.file("extdata","predictors_chile.tif", package="CAST"))

  set.seed(100)
  model <- caret::train(
    splotdata[,6:16],
    splotdata$Species_richness,
    ntree = 10,
    trControl = caret::trainControl(method = "cv", savePredictions = TRUE)
  )

  AOA <- CAST::aoa(predictors, model, verbose = FALSE)
  errormodel_DI <- CAST::errorProfiles(model, AOA, variable = "DI")

  expect_s3_class(plot(errormodel_DI), "ggplot")
})


test_that("errorProfiles works with geodist", {
  skip_on_cran()
  skip_on_os("mac", arch = "aarch64")
  skip_if_not_installed("randomForest")
  skip_if_not_installed("scam")
  data(splotdata)
  predictors <- terra::rast(system.file("extdata","predictors_chile.tif", package="CAST"))

  set.seed(100)
  knndm_folds <- knndm(splotdata, modeldomain = predictors,dist_fun = "great_circle",dist_space="geographical")
  ctrl <- caret::trainControl(method="cv",
                       index=knndm_folds$indx_train,
                       savePredictions=TRUE)
  model <- caret::train(sf::st_drop_geometry(splotdata[,6:16]), sf::st_drop_geometry(splotdata$Species_richness), ntree = 10,
                        trControl = ctrl)

  AOA <- CAST::aoa(predictors, model,verbose=F)

  # geodist ~ error
  errormodel_geodist <- errorProfiles(model, AOA, locations=splotdata, variable = "geodist")


  #test model fit:
  expect_equal(round(as.numeric(summary(errormodel_geodist$fitted.values)),2),
               c(24.65, 25.97, 27.11, 28.11, 30.07, 33.93))

})
