#' Diagnostic Plots for Adaptive Type-I Progressive Hybrid Censoring Scheme
#'
#' Generates 3-panel diagnostic graphics (Histogram with density overlay, Dot plot, and ACF plot)
#' under Adaptive Type-I PHCS.
#'
#' @param input Numeric vector of failure times, dataset list, or fit object.
#' @param T_thresh Time threshold T. If NULL, extracted from input list if available.
#' @param pdf Function (optional), probability density function pdf(x, par) for density overlay.
#' @param par Numeric vector (optional), parameters for PDF overlay.
#' @param main Title prefix for plots.
#' @param ... Additional graphic parameters.
#'
#' @return Invisibly returns NULL. Displays diagnostic plots.
#' @export
#' @examples
#' set.seed(123)
#' my_cdf <- function(x, par) pexp(x, rate = par[1])
#' my_pdf <- function(x, par) dexp(x, rate = par[1])
#' R_plan <- c(rep(1, 8), rep(0, 4))
#' dat_gen <- r_adapt1_phcs(
#'   n = 20, m = 12, T_thresh = 2.5,
#'   R = R_plan, pdf = my_pdf,
#'   cdf = my_cdf, par = 0.5
#' )
#' plot_adapt1_phcs(
#'   dat_gen, pdf = my_pdf, par = 0.5
#' )
plot_adapt1_phcs <- function(input, T_thresh = NULL, pdf = NULL, par = NULL, main = "Adaptive Type-I PHCS Diagnostics", ...) {
  if (is.list(input) && !is.null(input$data)) {
    data <- input$data
    if (is.null(T_thresh)) T_thresh <- input$T_thresh
  } else if (inherits(input, "mle_adapt_fit")) {
    data <- input$data
    if (is.null(T_thresh)) T_thresh <- input$T_thresh
    if (is.null(pdf)) pdf <- input$pdf
    if (is.null(par)) par <- input$par
  } else if (is.numeric(input)) {
    data <- input
  } else {
    stop("Invalid input type for plot_adapt1_phcs.")
  }
  
  old_par <- graphics::par(no.readonly = TRUE)
  on.exit(graphics::par(old_par))
  
  graphics::par(mfrow = c(1, 3), mar = c(4.5, 4.5, 3, 1))
  
  h <- graphics::hist(data, prob = TRUE, main = paste(main, "\n(Histogram)"), xlab = "Failure Times", col = "skyblue", border = "white")
  if (!is.null(pdf) && !is.null(par)) {
    x_grid <- seq(min(data, 1e-3), max(data, T_thresh %||% max(data)), length.out = 200)
    y_grid <- pdf(x_grid, par)
    graphics::lines(x_grid, y_grid, col = "red", lwd = 2)
  }
  
  plot(data, rep(1, length(data)), pch = 19, col = "navy", ylab = "", xlab = "Observed Lifetimes", yaxt = "n", main = paste(main, "\n(Dot Plot)"))
  if (!is.null(T_thresh)) {
    graphics::abline(v = T_thresh, col = "darkorange", lwd = 2, lty = 2)
  }
  
  spacings <- diff(c(0, sort(data)))
  stats::acf(spacings, main = paste(main, "\n(ACF of Spacings)"), col = "darkgreen")
  
  invisible(NULL)
}

#' Diagnostic Plots for Adaptive Type-II Progressive Hybrid Censoring Scheme
#'
#' Generates 3-panel diagnostic graphics (Histogram with density overlay, Dot plot, and ACF plot)
#' under Adaptive Type-II PHCS.
#'
#' @param input Numeric vector of failure times, dataset list, or fit object.
#' @param T_thresh Time threshold T. If NULL, extracted from input list if available.
#' @param pdf Function (optional), probability density function pdf(x, par) for density overlay.
#' @param par Numeric vector (optional), parameters for PDF overlay.
#' @param main Title prefix for plots.
#' @param ... Additional graphic parameters.
#'
#' @return Invisibly returns NULL. Displays diagnostic plots.
#' @export
#' @examples
#' set.seed(123)
#' my_cdf <- function(x, par) pexp(x, rate = par[1])
#' my_pdf <- function(x, par) dexp(x, rate = par[1])
#' R_plan <- c(rep(1, 8), rep(0, 4))
#' dat_gen <- r_adapt2_phcs(
#'   n = 20, m = 12, T_thresh = 2.5,
#'   R = R_plan, pdf = my_pdf,
#'   cdf = my_cdf, par = 0.5
#' )
#' plot_adapt2_phcs(
#'   dat_gen, pdf = my_pdf, par = 0.5
#' )
plot_adapt2_phcs <- function(input, T_thresh = NULL, pdf = NULL, par = NULL, main = "Adaptive Type-II PHCS Diagnostics", ...) {
  if (is.list(input) && !is.null(input$data)) {
    data <- input$data
    if (is.null(T_thresh)) T_thresh <- input$T_thresh
  } else if (inherits(input, "mle_adapt_fit")) {
    data <- input$data
    if (is.null(T_thresh)) T_thresh <- input$T_thresh
    if (is.null(pdf)) pdf <- input$pdf
    if (is.null(par)) par <- input$par
  } else if (is.numeric(input)) {
    data <- input
  } else {
    stop("Invalid input type for plot_adapt2_phcs.")
  }
  
  old_par <- graphics::par(no.readonly = TRUE)
  on.exit(graphics::par(old_par))
  
  graphics::par(mfrow = c(1, 3), mar = c(4.5, 4.5, 3, 1))
  
  h <- graphics::hist(data, prob = TRUE, main = paste(main, "\n(Histogram)"), xlab = "Failure Times", col = "lightgreen", border = "white")
  if (!is.null(pdf) && !is.null(par)) {
    x_grid <- seq(min(data, 1e-3), max(data, T_thresh %||% max(data)), length.out = 200)
    y_grid <- pdf(x_grid, par)
    graphics::lines(x_grid, y_grid, col = "darkred", lwd = 2)
  }
  
  plot(data, rep(1, length(data)), pch = 19, col = "purple", ylab = "", xlab = "Observed Lifetimes", yaxt = "n", main = paste(main, "\n(Dot Plot)"))
  if (!is.null(T_thresh)) {
    graphics::abline(v = T_thresh, col = "darkorange", lwd = 2, lty = 2)
  }
  
  spacings <- diff(c(0, sort(data)))
  stats::acf(spacings, main = paste(main, "\n(ACF of Spacings)"), col = "blue")
  
  invisible(NULL)
}

`%||%` <- function(x, y) if (is.null(x)) y else x
