#' Reliability and Lifetime Analysis for Step-Stress Accelerated Degradation Models
#'
#' Computes the reliability function R(t), cumulative failure distribution F(t), and Mean Time To Failure (MTTF)
#' at use stress level S0 and failure threshold omega_F based on fitted Wiener or Gamma process SSADT parameters.
#'
#' @param t Numeric vector of time points at which to evaluate reliability.
#' @param process Character string specifying the process type: \code{"wiener"} or \code{"gamma"}.
#' @param model Character string or function for acceleration model (\code{"arrhenius"} or \code{"power"}).
#' @param params Vector of estimated model parameters: \code{c(a, b, sigma)} for Wiener, or \code{c(a, b, u)} for Gamma.
#' @param S0 Normal use-stress level.
#' @param omega_F Failure threshold for degradation.
#'
#' @return A list of class \code{"ssad_reliability"} containing:
#'   \item{time}{Vector of time points.}
#'   \item{reliability}{Vector of reliability values R(t).}
#'   \item{cdf}{Vector of cumulative distribution function values F(t).}
#'   \item{mttf}{Mean Time To Failure under use stress S0.}
#'   \item{process}{Degradation process type.}
#'   \item{S0}{Use stress level.}
#'   \item{omega_F}{Failure threshold.}
#'
#' @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
#' # Wiener-Arrhenius model example
#' rel_wiener <- ssad_reliability(
#'   t = seq(100, 10000, by = 100),
#'   process = "wiener",
#'   model = "arrhenius",
#'   params = c(a = 5.4188, b = -2564.3, sigma = 0.0894),
#'   S0 = 25,
#'   omega_F = 200
#' )
#' print(rel_wiener$mttf)
ssad_reliability <- function(t, process = c("wiener", "gamma"), model = c("arrhenius", "power"),
                             params, S0, omega_F) {
  process <- match.arg(process)
  if (is.character(model)) {
    model <- match.arg(model)
  }
  
  eval_acc <- function(S, m_type, par) {
    if (is.function(m_type)) {
      return(m_type(S, par))
    }
    a <- par[1]
    b <- par[2]
    if (m_type == "arrhenius") {
      return(exp(a + b / (273 + S)))
    } else if (m_type == "power") {
      return(a * (S^b))
    } else {
      stop("Unsupported acceleration model.")
    }
  }
  
  if (process == "wiener") {
    beta0 <- eval_acc(S0, model, params[1:2])
    sigma <- params[3]
    mu0 <- omega_F / beta0
    lambda0 <- (omega_F^2) / (sigma^2)
    
    # Cumulative failure distribution F(t) for Inverse Gaussian
    t_clean <- pmax(t, 1e-8)
    z1 <- sqrt(lambda0 / t_clean) * (t_clean / mu0 - 1)
    z2 <- -sqrt(lambda0 / t_clean) * (t_clean / mu0 + 1)
    
    exp_factor <- min(2 * lambda0 / mu0, 500) # prevent numeric overflow
    cdf_val <- stats::pnorm(z1) + exp(exp_factor) * stats::pnorm(z2)
    cdf_val <- pmin(pmax(cdf_val, 0), 1)
    rel_val <- 1 - cdf_val
    mttf_val <- mu0
    
  } else if (process == "gamma") {
    v0 <- eval_acc(S0, model, params[1:2])
    u <- params[3]
    
    alpha0 <- 1 / sqrt(u * omega_F)
    beta0_star <- (u * omega_F) / v0
    
    t_clean <- pmax(t, 1e-8)
    z <- (1 / alpha0) * (sqrt(t_clean / beta0_star) - sqrt(beta0_star / t_clean))
    cdf_val <- stats::pnorm(z)
    cdf_val <- pmin(pmax(cdf_val, 0), 1)
    rel_val <- 1 - cdf_val
    mttf_val <- beta0_star * (1 + (alpha0^2) / 2)
  }
  
  res <- list(
    time = t,
    reliability = rel_val,
    cdf = cdf_val,
    mttf = mttf_val,
    process = process,
    S0 = S0,
    omega_F = omega_F
  )
  class(res) <- "ssad_reliability"
  return(res)
}
