# WARNING - Generated by {fusen} from dev/flat_kwb_simulate_inflow.Rmd: do not edit by hand # nolint: line_length_linter.

test_that("create_random_distribution works correctly", {
  # Deterministic RNG so the range checks below are reproducible. Bounds are set
  # to +/- 5 sd: wide enough that valid draws never fall outside them, so the
  # test asserts the distribution's location/scale family, not an exact draw.
  set.seed(42)

  # Test with default parameters (uniform)
  result <- create_random_distribution()
  expect_equal(nrow(result$events), 365)
  expect_equal(ncol(result$events), 3)
  expect_equal(ncol(result$paras), 5)
  expect_true(all(result$events$values >= 10 & result$events$values <= 1000, na.rm = TRUE))

  # Test with log10_uniform
  result <- create_random_distribution(type = "log10_uniform", min = 1, max = 100)
  expect_true(all(result$events$values >= 1 & result$events$values <= 100, na.rm = TRUE))

  # Test with triangle
  result <- create_random_distribution(type = "triangle", min = 0, max = 100, mode = 50)
  expect_true(all(result$events$values >= 0 & result$events$values <= 100, na.rm = TRUE))

  # Test with norm (mean +/- 5 sd)
  result <- create_random_distribution(type = "norm", mean = 50, sdev = 10)
  expect_true(all(result$events$values >= 50 - 5 * 10 & result$events$values <= 50 + 5 * 10, na.rm = TRUE))

  # Test with log10_norm (exponent within mean +/- 5 sd)
  result <- create_random_distribution(type = "log10_norm", log10_mean = 2, log10_sdev = 0.5)
  expect_true(all(result$events$values >= 10^(2 - 5 * 0.5) & result$events$values <= 10^(2 + 5 * 0.5), na.rm = TRUE))

  # Test with lognorm (log within meanlog +/- 5 sdlog)
  result <- create_random_distribution(type = "lognorm", meanlog = 2, sdlog = 0.5)
  expect_true(all(result$events$values >= exp(2 - 5 * 0.5) & result$events$values <= exp(2 + 5 * 0.5), na.rm = TRUE))

  # Test with value
  result <- create_random_distribution(type = "value", value = 42)
  expect_true(all(result$events$values == 42, na.rm = TRUE))

  # Test with custom number_of_repeatings and number_of_events
  result <- create_random_distribution(number_of_repeatings = 2, number_of_events = 10)
  expect_equal(nrow(result$events), 20)
  expect_equal(length(unique(result$events$repeatID)), 2)
  expect_equal(length(unique(result$events$eventID)), 10)

  # Test with debug = FALSE
  result <- create_random_distribution(debug = FALSE)
  expect_equal(nrow(result$events), 365)

  # Test with invalid type
  expect_error(create_random_distribution(type = "invalid_type"), "is not an implemented distribution")
})
