# Cross-language data-layer parity: replay the shared golden tick stream through
# the TickAggregator and check the candles against the Rust reference, with and
# without gap filling. Fixtures are generated by
# `cargo run -p wickra-examples --bin gen_golden`.

find_data_golden_dir <- function() {
  d <- normalizePath(".", mustWork = FALSE)
  repeat {
    g <- file.path(d, "testdata", "golden")
    if (dir.exists(g)) {
      return(g)
    }
    parent <- dirname(d)
    if (identical(parent, d)) {
      return(NULL)
    }
    d <- parent
  }
}

test_that("tick aggregator matches the golden candles", {
  gdir <- find_data_golden_dir()
  skip_if(is.null(gdir), "golden fixtures not bundled with the package")

  read_mat <- function(name) {
    lines <- readLines(file.path(gdir, paste0(name, ".csv")))[-1]
    lines <- lines[nzchar(lines)]
    if (length(lines) == 0) {
      return(matrix(numeric(0), 0, 6))
    }
    do.call(rbind, lapply(lines, function(l) as.numeric(strsplit(l, ",")[[1]])))
  }

  ticks <- read_mat("data_ticks")
  specs <- list(
    list(gap = FALSE, fixture = "data_candles"),
    list(gap = TRUE, fixture = "data_candles_gap")
  )
  for (spec in specs) {
    agg <- TickAggregator(1000, spec$gap)
    got <- matrix(numeric(0), 0, 6)
    for (i in seq_len(nrow(ticks))) {
      out <- push(agg, ticks[i, 1], ticks[i, 2], ticks[i, 3])
      if (nrow(out) > 0) {
        got <- rbind(got, unname(out))
      }
    }
    want <- unname(read_mat(spec$fixture))
    expect_equal(nrow(got), nrow(want), info = spec$fixture)
    expect_equal(got, want, tolerance = 1e-9, info = spec$fixture)
  }
})

test_that("candle reader matches the golden candles", {
  gdir <- find_data_golden_dir()
  skip_if(is.null(gdir), "golden fixtures not bundled with the package")

  read_mat <- function(name) {
    lines <- readLines(file.path(gdir, paste0(name, ".csv")))[-1]
    lines <- lines[nzchar(lines)]
    do.call(rbind, lapply(lines, function(l) as.numeric(strsplit(l, ",")[[1]])))
  }

  csv <- paste(readLines(file.path(gdir, "data_csv.csv")), collapse = "\n")
  reader <- CandleReader(csv)
  got <- unname(read(reader))
  want <- unname(read_mat("data_csv_candles"))
  expect_equal(nrow(got), nrow(want))
  expect_equal(got, want, tolerance = 1e-9)
})

test_that("resampler matches the golden candles", {
  gdir <- find_data_golden_dir()
  skip_if(is.null(gdir), "golden fixtures not bundled with the package")

  read_mat <- function(name) {
    lines <- readLines(file.path(gdir, paste0(name, ".csv")))[-1]
    lines <- lines[nzchar(lines)]
    do.call(rbind, lapply(lines, function(l) as.numeric(strsplit(l, ",")[[1]])))
  }

  input <- read_mat("input") # open,high,low,close,volume (timestamp = row index)
  r <- Resampler(5)
  got <- matrix(numeric(0), 0, 6)
  for (i in seq_len(nrow(input))) {
    c <- update(r, input[i, 1], input[i, 2], input[i, 3], input[i, 4], input[i, 5], i - 1)
    if (!is.na(c[1])) {
      got <- rbind(got, unname(c))
    }
  }
  f <- flush(r)
  if (!is.null(f)) {
    got <- rbind(got, unname(f))
  }
  want <- unname(read_mat("data_resampled"))
  expect_equal(nrow(got), nrow(want))
  expect_equal(got, want, tolerance = 1e-9)
})
