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

test_that("generate_random_values 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)

  # Create a sample config object
  config <- list(
    type = "norm",
    value = 10,
    min = 5,
    max = 15,
    mean = 10,
    sd = 2,
    meanlog = log(10),
    sdlog = 0.5,
    mode = 10
  )

  # Test with default parameters
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)
  expect_equal(ncol(result$events), 3)
  expect_equal(ncol(result$paras), 5)
  expect_true(all(result$events$values >= config$mean - 5 * config$sd & result$events$values <= config$mean + 5 * config$sd, na.rm = TRUE))

  # Test with different distribution type (lognorm)
  config$type <- "lognorm"
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)
  expect_true(all(result$events$values >= exp(config$meanlog - 5*config$sdlog) &
                 result$events$values <= exp(config$meanlog + 5*config$sdlog), na.rm = TRUE))

  # Test with different distribution type (triangle)
  config$type <- "triangle"
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)
  expect_true(all(result$events$values >= config$min & result$events$values <= config$max, na.rm = TRUE))

  # Test with different distribution type (value)
  config$type <- "value"
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)
  expect_true(all(result$events$values == config$value, na.rm = TRUE))

  # Test with custom number_of_repeatings and number_of_events
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 2,
    number_of_events = 10,
    debug = FALSE
  )
  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 = TRUE (should not cause errors)
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = TRUE
  )
  expect_equal(nrow(result$events), 365)

  # Test with missing mean and sd for norm distribution
  config$type <- "norm"
  config$mean <- NA
  config$sd <- NA
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)
  # mean defaults to (min + max)/2 = 10; generous fixed band, deterministic via seed
  expect_true(all(result$events$values >= 0 & result$events$values <= 20, na.rm = TRUE))

  # Test with missing meanlog and sdlog for lognorm distribution
  config$type <- "lognorm"
  config$meanlog <- NA
  config$sdlog <- NA
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)

  # Test with missing mode for triangle distribution
  config$type <- "triangle"
  config$mode <- NA
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)
  expect_true(all(result$events$values >= config$min & result$events$values <= config$max, na.rm = TRUE))

  # Test with min == max for triangle distribution (should switch to uniform)
  config$type <- "triangle"
  config$min <- 10
  config$max <- 10
  result <- generate_random_values(
    config = config,
    number_of_repeatings = 1,
    number_of_events = 365,
    debug = FALSE
  )
  expect_equal(nrow(result$events), 365)
  expect_true(all(result$events$values == 10, na.rm = TRUE))
})
