## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(rMIDAS2)
can_mock <- requireNamespace("jsonlite", quietly = TRUE)

## ----mocked-example, eval = can_mock------------------------------------------
mock_json_response <- function(body, status = 200L) {
  function(req) {
    httr2::response(
      status_code = status,
      headers = list("Content-Type" = "application/json"),
      body = charToRaw(jsonlite::toJSON(body, auto_unbox = TRUE))
    )
  }
}

pkg_env <- rMIDAS2:::.pkg_env
old_process <- pkg_env$process
old_port <- pkg_env$port
old_base_url <- pkg_env$base_url
on.exit({
  pkg_env$process <- old_process
  pkg_env$port <- old_port
  pkg_env$base_url <- old_base_url
}, add = TRUE)

pkg_env$process <- list(is_alive = function() TRUE)
pkg_env$port <- 9999L
pkg_env$base_url <- "http://127.0.0.1:9999"

example_data <- data.frame(
  Y = c(1.2, -0.4, 0.7),
  X1 = c(NA, 0.5, -1.1),
  X2 = c(0.3, 1.4, -0.2)
)

mock_complete <- mock_json_response(list(
  model_id = "mod-001",
  m = 2,
  columns = list("Y", "X1", "X2"),
  imputations = list(
    list(list(1.2, 0.1, 0.3), list(-0.4, 0.5, 1.4), list(0.7, -1.1, -0.2)),
    list(list(1.2, 0.2, 0.3), list(-0.4, 0.5, 1.4), list(0.7, -1.1, -0.2))
  )
))

result <- httr2::with_mocked_responses(mock_complete, {
  midas(example_data, m = 2, epochs = 1)
})

result$model_id
head(result$imputations[[1]])

## ----install, eval = FALSE----------------------------------------------------
# install_backend(method = "pip")

## ----data---------------------------------------------------------------------
set.seed(42)
n <- 500
df <- data.frame(
  Y = rnorm(n),
  X1 = rnorm(n),
  X2 = rnorm(n)
)
df$X1[df$X2 > 0.5] <- NA
head(df)

## ----midas, eval = FALSE------------------------------------------------------
# result <- midas(df, m = 10, epochs = 20)
# 
# # View first imputed dataset
# head(result$imputations[[1]])

## ----stepwise, eval = FALSE---------------------------------------------------
# # Fit the model
# fit <- midas_fit(df, epochs = 20)
# 
# # Generate imputations — pass the fitted object directly
# imps <- midas_transform(fit, m = 10)
# 
# head(imps[[1]])

## ----mean, eval = FALSE-------------------------------------------------------
# mean_df <- imp_mean(fit)
# head(mean_df)

## ----combine, eval = FALSE----------------------------------------------------
# reg <- combine(fit, y = "Y")
# reg

## ----overimpute, eval = FALSE-------------------------------------------------
# diag <- overimpute(fit, mask_frac = 0.1)
# diag$mean_rmse
# diag$rmse

## ----stop, eval = FALSE-------------------------------------------------------
# stop_server()

