library(testthat)
library(AdaptHyCensor)

test_that("Data generation functions work cleanly without NAs or warnings", {
  set.seed(42)
  my_cdf <- function(x, par) pexp(x, rate = par[1])
  my_pdf <- function(x, par) dexp(x, rate = par[1])
  my_qdf <- function(p, par) qexp(p, rate = par[1])
  
  res1 <- r_adapt1_phcs(n = 20, m = 12, T_thresh = 2.5, R = c(rep(1, 8), rep(0, 4)),
                        pdf = my_pdf, cdf = my_cdf, qdf = my_qdf, par = 0.5)
  expect_true(length(res1$data) > 0)
  expect_false(any(is.na(res1$data)))
  expect_false(any(is.nan(res1$data)))
  
  res2 <- r_adapt2_phcs(n = 20, m = 12, T_thresh = 2.5, R = c(rep(1, 8), rep(0, 4)),
                        pdf = my_pdf, cdf = my_cdf, qdf = my_qdf, par = 0.5)
  expect_equal(length(res2$data), 12)
  expect_false(any(is.na(res2$data)))
  expect_false(any(is.nan(res2$data)))
})

test_that("MLE estimation works across all supported optimization methods", {
  set.seed(42)
  my_cdf <- function(x, par) pexp(x, rate = par[1])
  my_pdf <- function(x, par) dexp(x, rate = par[1])
  my_surf <- function(x, par) 1 - pexp(x, rate = par[1])
  
  dat1 <- r_adapt1_phcs(n = 20, m = 12, T_thresh = 2.5, R = c(rep(1, 8), rep(0, 4)),
                        pdf = my_pdf, cdf = my_cdf, par = 0.5)$data
  
  methods_to_test <- c("BFGS", "NR", "NM", "CG", "BHHH")
  
  for (mth in methods_to_test) {
    fit1 <- mle_adapt1_phcs(data = dat1, n = 20, m = 12, T_thresh = 2.5,
                            R = c(rep(1, 8), rep(0, 4)), pdf = my_pdf, cdf = my_cdf,
                            surf = my_surf, start = c(rate = 0.5), lower = c(rate = 0.01), method = mth)
    expect_true(is.finite(fit1$par[1]))
    expect_false(is.na(fit1$par[1]))
    expect_true(fit1$par[1] > 0)
  }
})

test_that("Bayes MCMC, Importance Sampling, and Lindley approximation work", {
  set.seed(42)
  my_cdf <- function(x, par) pexp(x, rate = par[1])
  my_pdf <- function(x, par) dexp(x, rate = par[1])
  my_surf <- function(x, par) 1 - pexp(x, rate = par[1])
  flat_prior <- function(par) ifelse(par[1] > 0, 0, -Inf)
  
  dat2 <- r_adapt2_phcs(n = 20, m = 12, T_thresh = 2.5, R = c(rep(1, 8), rep(0, 4)),
                        pdf = my_pdf, cdf = my_cdf, par = 0.5)$data
  
  # Bayes
  b_res <- bayes_adapt2_phcs(data = dat2, n = 20, m = 12, T_thresh = 2.5,
                             R = c(rep(1, 8), rep(0, 4)), pdf = my_pdf, cdf = my_cdf,
                             surf = my_surf, prior = flat_prior, start = c(rate = 0.5),
                             lower = c(rate = 0.01), N_iter = 500, burn_in = 100)
  expect_true(is.numeric(b_res$summary))
  
  # IS
  is_res <- is_adapt2_phcs(data = dat2, n = 20, m = 12, T_thresh = 2.5,
                           R = c(rep(1, 8), rep(0, 4)), pdf = my_pdf, cdf = my_cdf,
                           surf = my_surf, prior = flat_prior, start = c(rate = 0.5),
                           lower = c(rate = 0.01), N_samples = 500)
  expect_true(is_res$ESS > 0)
  
  # Lindley
  lind_res <- lindley_adapt2_phcs(data = dat2, n = 20, m = 12, T_thresh = 2.5,
                                  R = c(rep(1, 8), rep(0, 4)), pdf = my_pdf, cdf = my_cdf,
                                  surf = my_surf, prior = flat_prior, start = c(rate = 0.5),
                                  lower = c(rate = 0.01))
  expect_true(is.numeric(lind_res$estimates))
})
