#' @importFrom stats integrate rnorm sd
#'
#' @title Self-Similarity Test for Normality (SSTN)
#'
#' @description The SSTN is a statistical test for assessing whether a given sample originates
#' from a normal distribution. It is based on the iterative application of the
#' empirical characteristic function and comparing these estimated characteristic functions.
#' A Monte Carlo procedure is used to obtain the empirical distribution of the test statistic
#' under the null hypothesis.
#'
#' @param x Numeric vector of observations \eqn{(x_1, \dots, x_n)} drawn from
#'   a distribution with finite variance. Must have length \eqn{n \ge 2}.
#' @param verbose Logical. If TRUE (default), prints a summary of the test results
#'   including the number of summands, test statistic, and \eqn{p}-value.
#'
#' @return An invisible list with the following components:
#' \item{test.statistic}{Numeric. The observed value of the SSTN test statistic.}
#' \item{method}{Character string indicating whether the asymptotic or the finite-sample
#' null distribution was used.}
#' \item{p.value}{Numeric. The \eqn{p}-value of the test.}
#'
#' @author Akin Anarat \email{akin.anarat@hhu.de}
#'
#' @references
#' Anarat, A. and Schwender, H. (2026).
#' A test for normality based on self-similarity.
#' arXiv preprint <doi:10.48550/arXiv.2604.03810>.
#'
#'
#' @export
#'
#' @examples
#' set.seed(123)
#' # Sample from standard normal (null hypothesis true)
#' x <- rnorm(100)
#' res <- sstn(x)
#' res$p.value
#'
#' # Sample from Gamma distribution (null hypothesis false)
#' y <- rgamma(100, 1)
#' res2 <- sstn(y)
#' res2$p.value

sstn <- function(x, verbose = FALSE){

  ns <- asNamespace("sstn")

  n <- length(x)

  if (!is.numeric(x) || n < 2) stop("Condition violated: n >= 2")

  weight.type <- "normal"
  beta <- 2
  M <- 20
  t_max <- 4
  H <- 100

  use.asymptotic <- (n >= 100)
  if (verbose && use.asymptotic)
    message("Using asymptotic T (n >= 100).")

  if (use.asymptotic) {

    calib  <- get("calib", envir = ns)
    T.vals <- get("T_asy", envir = ns)

    mu    <- calib$mu
    sigma <- calib$sigma
    H     <- length(calib$t)

    method <- "SSTN (asymptotic null distribution)"
  }else {
    if (verbose) message("Computing exact finite-sample null distribution T^(n).")

    T.obj <- paste0("T_n_", n)

    T.values <- get(T.obj, envir = ns)

    mu <- T.values$mu
    sigma <- T.values$sigma
    T.vals <- T.values$T.vals

    method <- "SSTN (finite-sample null distribution)"
  }

  T_obs <- compute_T_obs(x, mu = mu, sigma = sigma, M = M, t_max = t_max,
                         H = H, weight.type = weight.type, beta = beta)

  p.value <- mean(T.vals >= T_obs)

  if (verbose){
    cat("SSTN Test\n")
    cat("--------------------------------------------------\n")
    cat(sprintf("Method: %s\n", method))
    cat(sprintf("Test statistic (reported): %.5f\n", T_obs))
    cat(sprintf("p-value: %.5f\n", p.value))
  }

  return(invisible(list(
    method           = method,
    test.statistic   = T_obs,
    p.value          = p.value
  )))
}
