test_that("ratio deprecation", {
  expect_snapshot(
    error = TRUE,
    new_rec <- recipe(~., data = circle_example) |>
      step_downsample(class, ratio = 2)
  )
})

test_that("basic usage", {
  rec1 <- recipe(~., data = circle_example) |>
    step_downsample(class)

  rec1_p <- prep(rec1)

  te_xtab <- table(bake(rec1_p, new_data = circle_example)$class, useNA = "no")
  og_xtab <- table(circle_example$class, useNA = "no")

  expect_equal(sort(te_xtab), sort(og_xtab))

  expect_no_warning(prep(rec1))
})

test_that("works with a single predictor", {
  skip_if_not_installed("modeldata")

  data("hpc_data", package = "modeldata")

  expect_no_error(
    recipe(class ~ compounds, data = hpc_data) |>
      step_downsample(all_outcomes()) |>
      prep() |>
      bake(NULL)
  )
})

test_that("bad data", {
  rec <- recipe(~., data = circle_example)
  # numeric check
  expect_snapshot(
    error = TRUE,
    rec |>
      step_downsample(x) |>
      prep()
  )
  # Multiple variable check
  expect_snapshot(
    error = TRUE,
    rec |>
      step_downsample(class, id) |>
      prep()
  )
})

test_that("`seed` produces identical sampling", {
  step_with_seed <- function(seed = sample.int(10^5, 1)) {
    recipe(~., data = circle_example) |>
      step_downsample(class, seed = seed) |>
      prep() |>
      bake(new_data = NULL) |>
      pull(x)
  }

  run_1 <- step_with_seed(seed = 1234)
  run_2 <- step_with_seed(seed = 1234)
  run_3 <- step_with_seed(seed = 12345)

  expect_equal(run_1, run_2)
  expect_false(identical(run_1, run_3))
})

test_that("test tidy()", {
  rec <- recipe(~., data = circle_example) |>
    step_downsample(class, id = "")

  rec_p <- prep(rec)

  untrained <- tibble(
    terms = "class",
    id = ""
  )

  trained <- tibble(
    terms = "class",
    id = ""
  )

  expect_equal(untrained, tidy(rec, number = 1))
  expect_equal(trained, tidy(rec_p, number = 1))
})

test_that("ratio value works when undersampling", {
  res1 <- recipe(~., data = circle_example) |>
    step_downsample(class) |>
    prep() |>
    bake(new_data = NULL)

  res1.5 <- recipe(~., data = circle_example) |>
    step_downsample(class, under_ratio = 1.5) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(
    as.vector(table(res1$class)),
    rep(min(table(circle_example$class)), length(table(res1$class)))
  )
  expect_equal(
    sort(as.numeric(table(res1.5$class))),
    min(table(circle_example$class)) * c(1, 1.5)
  )
})

test_that("replacement = TRUE gives a bootstrapped under-sample", {
  res <- recipe(~., data = circle_example) |>
    step_downsample(class, replacement = TRUE, seed = 1) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(
    as.vector(table(res$class)),
    rep(min(table(circle_example$class)), length(table(res$class)))
  )
  expect_lt(nrow(dplyr::distinct(res)), nrow(res))
})

test_that("replacement = FALSE doesn't duplicate rows", {
  res <- recipe(~., data = circle_example) |>
    step_downsample(class, replacement = FALSE, seed = 1) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(nrow(dplyr::distinct(res)), nrow(res))
})

test_that("replacement = TRUE resamples the minority level too", {
  res <- recipe(~., data = circle_example) |>
    step_downsample(class, replacement = TRUE, seed = 1) |>
    prep() |>
    bake(new_data = NULL)

  minority <- names(which.min(table(circle_example$class)))
  minority_rows <- dplyr::filter(res, class == minority)

  expect_lt(nrow(dplyr::distinct(minority_rows)), nrow(minority_rows))
})

test_that("allows multi-class", {
  skip_if_not_installed("modeldata")

  data("credit_data", package = "modeldata")
  expect_no_error(
    recipe(Home ~ Age + Income + Assets, data = credit_data) |>
      step_impute_mean(Income, Assets) |>
      step_downsample(Home)
  )
})

test_that("minority classes are ignored if there is more than 1", {
  skip_if_not_installed("modeldata")

  data("penguins", package = "modeldata")
  rec1_p2 <- recipe(
    species ~ bill_length_mm + bill_depth_mm,
    data = penguins[-(1:84), ]
  ) |>
    step_impute_mean(all_predictors()) |>
    step_downsample(species) |>
    prep() |>
    bake(new_data = NULL)

  expect_true(all(max(table(rec1_p2$species)) == 68))
})

test_that("factor levels are not affected by alphabet ordering or class sizes", {
  circle_example_alt_levels <- list()
  for (i in 1:4) {
    circle_example_alt_levels[[i]] <- circle_example
  }

  # Checking for forgetting levels by majority/minor switching
  for (i in c(2, 4)) {
    levels(circle_example_alt_levels[[i]]$class) <-
      rev(levels(circle_example_alt_levels[[i]]$class))
  }

  # Checking for forgetting levels by alphabetical switching
  for (i in c(3, 4)) {
    circle_example_alt_levels[[i]]$class <-
      factor(
        x = circle_example_alt_levels[[i]]$class,
        levels = rev(levels(circle_example_alt_levels[[i]]$class))
      )
  }

  for (i in 1:4) {
    rec_p <- recipe(~., data = circle_example_alt_levels[[i]]) |>
      step_downsample(class) |>
      prep()

    expect_equal(
      levels(circle_example_alt_levels[[i]]$class), # Original levels
      rec_p$levels$class$values # New levels
    )
    expect_equal(
      levels(circle_example_alt_levels[[i]]$class), # Original levels
      levels(bake(rec_p, new_data = NULL)$class) # New levels
    )
  }
})


test_that("id variables don't turn predictors to factors", {
  # https://github.com/tidymodels/themis/issues/56
  rec_id <- recipe(class ~ ., data = circle_example) |>
    update_role(id, new_role = "id") |>
    step_downsample(class) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(is.double(rec_id$x), TRUE)
  expect_equal(is.double(rec_id$y), TRUE)
})

test_that("case_weights", {
  circle_example_cw <- circle_example |>
    mutate(weights = frequency_weights(rep(0:1, c(200, 200))))

  rec1_p <- recipe(~., data = circle_example_cw) |>
    step_downsample(class) |>
    prep()

  exp_count <- circle_example_cw |>
    filter(as.integer(weights) == 1) |>
    count(class) |>
    pull(n) |>
    min()

  rec_count <- bake(rec1_p, new_data = NULL) |>
    count(class) |>
    pull(n)

  expect_true(all(exp_count == rec_count))

  expect_snapshot(rec1_p)

  # ignore importance weights
  circle_example_cw <- circle_example |>
    mutate(weights = importance_weights(rep(0:1, c(200, 200))))

  rec1_p <- recipe(~., data = circle_example_cw) |>
    step_downsample(class) |>
    prep()

  exp_count <- circle_example_cw |>
    count(class) |>
    pull(n) |>
    min()

  rec_count <- bake(rec1_p, new_data = NULL) |>
    count(class) |>
    pull(n)

  expect_true(all(exp_count == rec_count))

  expect_snapshot(rec1_p)
})

test_that("tunable", {
  rec <- recipe(~., data = mtcars) |>
    step_downsample(all_predictors())
  rec_param <- tunable.step_downsample(rec$steps[[1]])
  expect_equal(rec_param$name, c("under_ratio"))
  expect_true(all(rec_param$source == "recipe"))
  expect_true(is.list(rec_param$call_info))
  expect_equal(nrow(rec_param), 1)
  expect_equal(
    names(rec_param),
    c("name", "call_info", "source", "component", "component_id")
  )
})

test_that("bad args", {
  expect_snapshot(
    error = TRUE,
    recipe(~., data = mtcars) |>
      step_downsample(under_ratio = "yes") |>
      prep()
  )
  expect_snapshot(
    error = TRUE,
    recipe(~., data = mtcars) |>
      step_downsample(seed = TRUE)
  )
  expect_snapshot(
    error = TRUE,
    recipe(~., data = mtcars) |>
      step_downsample(replacement = "yes")
  )
})

test_that("tunable is setup to works with extract_parameter_set_dials", {
  skip_if_not_installed("dials")
  rec <- recipe(~., data = mtcars) |>
    step_downsample(
      all_predictors(),
      under_ratio = hardhat::tune()
    )

  params <- extract_parameter_set_dials(rec)

  expect_s3_class(params, "parameters")
  expect_identical(nrow(params), 1L)
})

test_that("NA values in outcome are handled", {
  df <- data.frame(
    x = 1:10,
    class = factor(c(NA, "A", "A", "A", "A", "B", "B", "B", "B", "B"))
  )

  result <- recipe(~., data = df) |>
    step_downsample(class) |>
    prep() |>
    bake(new_data = NULL)

  expect_true(any(is.na(result$class)))
  # majority class should be downsampled to match minority
  expect_equal(
    length(unique(table(result$class, useNA = "no"))),
    1L
  )
})

test_that("unused outcome levels are skipped with a warning (#238)", {
  circle_example <- circle_example[, c("x", "y", "class")]
  circle_example$class <- factor(
    circle_example$class,
    levels = c(levels(circle_example$class), "unused")
  )

  expect_snapshot(
    res <- recipe(class ~ ., data = circle_example) |>
      step_downsample(class) |>
      prep() |>
      bake(new_data = NULL)
  )

  expect_gt(nrow(res), 0)
})

test_that("baking works on new_data (keys off column value)", {
  rec <- recipe(~., data = circle_example) |>
    step_downsample(class) |>
    prep()

  res <- bake(rec, new_data = circle_example)

  expect_equal(sort(table(res$class)), sort(table(circle_example$class)))
})

test_that("step_downsample() accepts a named `under_ratio` vector (#323)", {
  df <- data.frame(
    x = seq_len(70),
    class = factor(c(rep("a", 10), rep("b", 20), rep("c", 40)))
  )

  res <- recipe(class ~ x, data = df) |>
    step_downsample(class, under_ratio = c(b = 1.5, c = 2)) |>
    prep() |>
    bake(new_data = NULL)

  # Targets are ratios of the minority count (10); "a" is untouched
  expect_equal(as.numeric(table(res$class)), c(10, 15, 20))
})

test_that("step_downsample() with a constant vector matches the scalar (#323)", {
  df <- data.frame(
    x = seq_len(70),
    class = factor(c(rep("a", 10), rep("b", 20), rep("c", 40)))
  )

  bake_ratio <- function(ratio) {
    recipe(class ~ x, data = df) |>
      step_downsample(class, under_ratio = ratio, seed = 1) |>
      prep() |>
      bake(new_data = NULL)
  }

  expect_equal(
    bake_ratio(c(a = 1.5, b = 1.5, c = 1.5)),
    bake_ratio(1.5)
  )
})

test_that("step_downsample() leaves NA outcomes alone for a named vector (#323)", {
  df <- data.frame(
    x = seq_len(40),
    class = factor(c(rep("a", 10), rep("b", 20), rep(NA, 10)))
  )

  res <- recipe(~., data = df) |>
    step_downsample(class, under_ratio = c(b = 1)) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(as.numeric(table(res$class)), c(10, 10))
  expect_identical(sum(is.na(res$class)), 10L)
})

test_that("step_downsample() keeps unnamed levels intact with replacement (#323)", {
  df <- data.frame(
    x = seq_len(30),
    class = factor(c(rep("a", 10), rep("b", 20)))
  )

  res <- recipe(class ~ x, data = df) |>
    step_downsample(class, under_ratio = c(b = 1.5), replacement = TRUE) |>
    prep() |>
    bake(new_data = NULL)

  expect_identical(sort(res$x[res$class == "a"]), 1:10)
  expect_identical(sum(res$class == "b"), 15L)
})

test_that("step_downsample() errors on a bad `under_ratio` vector (#323)", {
  df <- data.frame(
    x = seq_len(30),
    class = factor(c(rep("a", 10), rep("b", 20)))
  )

  expect_snapshot(
    error = TRUE,
    recipe(class ~ x, data = df) |>
      step_downsample(class, under_ratio = c(a = 1, potato = 1)) |>
      prep()
  )

  expect_snapshot(
    error = TRUE,
    recipe(class ~ x, data = df) |>
      step_downsample(class, under_ratio = c(a = 1, a = 2)) |>
      prep()
  )
})

test_that("step_downsample() samples each class to its own target (#323)", {
  set.seed(1)
  df <- data.frame(
    x = rnorm(70),
    y = rnorm(70),
    class = factor(c(rep("a", 10), rep("b", 20), rep("c", 40)))
  )

  res <- recipe(class ~ ., data = df) |>
    step_downsample(class, under_ratio = c(b = 1.5, c = 2)) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(as.numeric(table(res$class)), c(10, 15, 20))
})

test_that("step_downsample() leaves a class alone when its target is on the wrong side (#323)", {
  set.seed(1)
  df <- data.frame(
    x = rnorm(70),
    y = rnorm(70),
    class = factor(c(rep("a", 10), rep("b", 20), rep("c", 40)))
  )

  res <- recipe(class ~ ., data = df) |>
    step_downsample(class, under_ratio = c(a = 5)) |>
    prep() |>
    bake(new_data = NULL)

  expect_equal(as.numeric(table(res$class)), c(10, 20, 40))
})

test_that("step_downsample() checks `under_ratio` names when prepped (#323)", {
  set.seed(1)
  df <- data.frame(
    x = rnorm(70),
    y = rnorm(70),
    class = factor(c(rep("a", 10), rep("b", 20), rep("c", 40)))
  )

  expect_snapshot(
    error = TRUE,
    recipe(class ~ ., data = df) |>
      step_downsample(class, under_ratio = c(a = 1, potato = 1)) |>
      prep()
  )
})

test_that("backwards compatible for arguments added after 1.0.3", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_downsample(class)

  exp <- bake(prep(rec), new_data = NULL)

  # simulates a recipe created by an older version of themis
  old <- rec
  old$steps[[1]]$replacement <- NULL

  expect_identical(bake(prep(old), new_data = NULL), exp)

  # simulates a recipe trained by an older version of themis
  old_trained <- prep(rec)
  old_trained$steps[[1]]$replacement <- NULL

  expect_identical(bake(old_trained, new_data = NULL), exp)
})

# Infrastructure ---------------------------------------------------------------

test_that("bake method errors when needed non-standard role columns are missing", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_downsample(class, skip = FALSE) |>
    add_role(class, new_role = "potato") |>
    update_role_requirements(role = "potato", bake = FALSE)

  trained <- prep(rec, training = circle_example, verbose = FALSE)

  expect_snapshot(
    error = TRUE,
    bake(trained, new_data = circle_example[, -3])
  )
})

test_that("empty printing", {
  rec <- recipe(mpg ~ ., mtcars)
  rec <- step_downsample(rec)

  expect_snapshot(rec)

  rec <- prep(rec, mtcars)

  expect_snapshot(rec)
})

test_that("empty selection prep/bake is a no-op", {
  rec1 <- recipe(mpg ~ ., mtcars)
  rec2 <- step_downsample(rec1)

  rec1 <- prep(rec1, mtcars)
  rec2 <- prep(rec2, mtcars)

  baked1 <- bake(rec1, mtcars)
  baked2 <- bake(rec2, mtcars)

  expect_identical(baked1, baked2)
})

test_that("empty selection tidy method works", {
  rec <- recipe(mpg ~ ., mtcars)
  rec <- step_downsample(rec)

  expect <- tibble(terms = character(), id = character())

  expect_identical(tidy(rec, number = 1), expect)

  rec <- prep(rec, mtcars)

  expect_identical(tidy(rec, number = 1), expect)
})

test_that("printing", {
  rec <- recipe(~., data = circle_example) |>
    step_downsample(class)

  expect_snapshot(print(rec))
  expect_snapshot(prep(rec))
})

test_that("0 and 1 rows data work in bake method", {
  rec <- recipe(class ~ x + y, data = circle_example) |>
    step_downsample(class, skip = FALSE) |>
    prep()

  expect_identical(nrow(bake(rec, new_data = slice(circle_example, 0))), 0L)
  expect_identical(nrow(bake(rec, new_data = slice(circle_example, 1))), 1L)
})
