#' Fit Multiple-Steps Step-Stress Accelerated Degradation Model
#'
#' Master fitting function for step-stress accelerated degradation testing (SSADT) models using
#' Wiener process or Gamma process degradation models with Arrhenius or Power acceleration relationships.
#'
#' @param data A data frame containing inspection times in the first column and unit degradation measurements in subsequent columns,
#'   or long-format data with columns \code{unit}, \code{time}, \code{degradation}.
#' @param process Character string specifying degradation process model: \code{"wiener"} or \code{"gamma"}.
#' @param model Acceleration model: \code{"arrhenius"} or \code{"power"}.
#' @param stress_levels Vector of stress levels S = (S1, S2, ..., SK). Default: \code{c(45, 65, 85)}.
#' @param thresholds Vector of degradation thresholds (omega_1, ..., omega_K-1) at which stress levels are elevated. Default: \code{c(90, 160)}.
#' @param method Estimation method: \code{"mle"} or \code{"mcmc"}. Default: \code{"mle"}.
#' @param init Initial parameter vector.
#' @param n_iter Number of MCMC iterations (default: 5000).
#' @param burnin Number of burn-in iterations (default: 1000).
#' @param thin Thinning interval for MCMC (default: 1).
#'
#' @return An S3 object of class \code{"ssadfit"} containing model fit results, parameter estimates, log-likelihood, and diagnostics.
#'
#' @references
#' Pan, Z., & Balakrishnan, N. (2010). Multiple-Steps Step-Stress Accelerated Degradation Modeling Based on Wiener and Gamma Processes.
#' \emph{Communications in Statistics - Simulation and Computation}, 39(7), 1384-1402. \doi{10.1080/03610918.2010.496060}
#'
#' @export
#' @examples
#' data(wiener_arrhenius)
#' fit <- ssad_fit(
#'   data = wiener_arrhenius,
#'   process = "wiener",
#'   model = "arrhenius",
#'   stress_levels = c(45, 65, 85),
#'   thresholds = c(90, 160),
#'   method = "mle"
#' )
#' print(fit)
#' summary(fit)
ssad_fit <- function(data, process = c("wiener", "gamma"), model = c("arrhenius", "power"),
                     stress_levels = c(45, 65, 85), thresholds = c(90, 160),
                     method = c("mle", "mcmc"), init = NULL, n_iter = 5000,
                     burnin = 1000, thin = 1) {
  process <- match.arg(process)
  model <- match.arg(model)
  method <- match.arg(method)
  
  if (process == "wiener") {
    res <- ssad_wiener(
      data = data,
      stress_levels = stress_levels,
      thresholds = thresholds,
      model = model,
      method = method,
      init = init,
      n_iter = n_iter,
      burnin = burnin,
      thin = thin
    )
  } else {
    res <- ssad_gamma(
      data = data,
      stress_levels = stress_levels,
      thresholds = thresholds,
      model = model,
      method = method,
      init = init,
      n_iter = n_iter,
      burnin = burnin,
      thin = thin
    )
  }
  
  return(res)
}
