#' Bootstrap BCa Confidence Interval for Correlation
#'
#' Computes confidence interval for the correlation of two populations
#' using bootstrap BCa (bias-corrected and accelerated) method.
#'
#' @param x Numeric vector of first variable (must have same length as y)
#' @param y Numeric vector of second variable (must have same length as x)
#' @param conf.level Confidence level for the interval (default 0.95)
#' @param R Number of bootstrap replicates (default 999)
#'
#' @return An S3 object of class "corrCI_boot" containing:
#'   \item{lower}{Lower bound of the confidence interval}
#'   \item{upper}{Upper bound of the confidence interval}
#'   \item{estimate}{Point estimate of correlation coefficient}
#'   \item{conf.level}{The confidence level}
#'   \item{method}{Description of the method}
#'
#' @details
#' This function implements the BCa (bias-corrected and accelerated) bootstrap
#' method for constructing confidence intervals for Pearson's correlation
#' coefficient. The BCa method adjusts for both bias and skewness in the
#' bootstrap distribution.
#'
#' The data is standardized in the same way as corrCI_sa for consistency
#' in comparison studies.
#'
#' @references
#' Efron, B. (1987). Better Bootstrap Confidence Intervals. Journal of the
#' American Statistical Association, 82(398), 171-185.
#'
#' @examples
#' set.seed(42)
#' x <- rnorm(30)
#' y <- x + rnorm(30, sd = 0.5)
#' result <- boot_corrCI(x, y)
#' print(result)
#'
#' @export
boot_corrCI <- function(x, y, conf.level = 0.95, R = 999) {

  # Input validation
  if (!is.numeric(x) || !is.numeric(y)) {
    stop("x and y must be numeric vectors")
  }
  if (length(x) != length(y)) {
    stop("x and y must have the same length")
  }
  if (length(x) < 3) {
    stop("x and y must have at least 3 observations")
  }
  if (conf.level <= 0 || conf.level >= 1) {
    stop("conf.level must be between 0 and 1")
  }
  if (R < 1 || R != floor(R)) {
    stop("R must be a positive integer")
  }

  numObs <- length(x)

  # Standardization (same as corrCI_sa)
  x_orig <- x
  y_orig <- y

  x <- (x_orig - mean(x_orig)) / sqrt(sum((x_orig - mean(x_orig))^2))
  y <- (y_orig - mean(y_orig)) / sqrt(sum((y_orig - mean(y_orig))^2))

  # Point estimate
  theta0 <- sum(x * y)

  # Combine data into two-column matrix for boot()
  data <- cbind(x, y)

  # Boot function to compute correlation coefficient
  boot_corr <- function(data, indices) {
    d <- data[indices, ]
    x_boot <- d[, 1]
    y_boot <- d[, 2]
    # Pearson correlation via sum of products (on standardized data)
    sum(x_boot * y_boot)
  }

  # Perform bootstrap
  boot_result <- boot::boot(data = data, statistic = boot_corr, R = R)

  # Get BCa confidence interval
  alpha <- 1 - conf.level
  boot_ci <- boot::boot.ci(boot_result, type = "bca", conf = conf.level)

  # Extract bounds
  lower <- boot_ci$bca[4]
  upper <- boot_ci$bca[5]

  # Build result object
  result <- list(
    lower = lower,
    upper = upper,
    estimate = theta0,
    conf.level = conf.level,
    method = "Bootstrap BCa Confidence Interval for Correlation"
  )

  class(result) <- "corrCI_boot"
  return(result)
}

#' Print Method for corrCI_boot Objects
#'
#' Prints the results of a bootstrap correlation confidence interval analysis.
#'
#' @param x An object of class "corrCI_boot"
#' @param ... Additional arguments passed to print
#'
#' @return Prints the confidence interval, estimate, and method information.
#'
#' @method print corrCI_boot
#'
#' @export
print.corrCI_boot <- function(x, ...) {
  cat("\n")
  cat("  Bootstrap BCa CI for Correlation\n")
  cat("  ----------------------------------------\n")
  cat(sprintf("  Estimate:   %.6f\n", x$estimate))
  cat(sprintf("  %g%% CI:    [ %.6f , %.6f ]\n",
              x$conf.level * 100, x$lower, x$upper))
  cat("  ----------------------------------------\n")
  cat(sprintf("  Method:     %s\n", x$method))
  cat("\n")
  invisible(x)
}