library(ggplot2)
library(dplyr)
library(readr)
library(stringr)
library(tidyr)

# Usage: Rscript combined_fault_correlation.r exp_abspath1 exp_abspath2 ... [resultsdata_file]
# Plots correlation between raw aot and interp fault counts (no C baseline).

args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 2) {
  stop("Need at least 2 experiments")
}

csv_suffix <- if (grepl("\\.csv$", args[length(args)])) {
  args[length(args)]
} else {
  "resultsdata.csv"
}
exp_args <- if (grepl("\\.csv$", args[length(args)])) {
  args[-length(args)]
} else {
  args
}

extract_info <- function(path) {
  dir_name <- basename(path)
  match <- str_match(
    dir_name,
    "^\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}_(.+?)_(c|aot|interp)_"
  )
  if (is.na(match[1, 1])) {
    warning(paste("Could not parse:", dir_name))
    return(NULL)
  }
  list(base_name = match[1, 2], variant = match[1, 3], path = path)
}

all_data <- data.frame()

for (arg in exp_args) {
  info <- extract_info(arg)
  if (is.null(info)) {
    next
  }

  csv_file <- file.path(info$path, csv_suffix)
  if (!file.exists(csv_file)) {
    warning(paste("Missing:", csv_file))
    next
  }

  df <- read_csv(csv_file, col_types = cols())
  df$base_name <- info$base_name
  df$variant <- info$variant
  all_data <- bind_rows(all_data, df)
}

if (nrow(all_data) == 0) {
  stop("No data loaded")
}

# Ignore OK_MARKER (only plot failures) and sum GROUP1_MARKER with TRAP
all_data <- all_data |>
  filter(resulttype != "OK_MARKER") |>
  mutate(resulttype = ifelse(resulttype == "GROUP1_MARKER", "TRAP", resulttype))

all_data <- all_data |>
  group_by(base_name, variant, benchmark, resulttype) |>
  summarise(faults = sum(faults), .groups = "drop")

# Only aot/interp matter; C is not used as a baseline here.
counts <- all_data |> filter(variant %in% c("aot", "interp"))

# Pivot to get aot and interp fault counts side by side
counts_wide <- counts |>
  select(base_name, benchmark, resulttype, variant, faults) |>
  pivot_wider(names_from = variant, values_from = faults) |>
  filter(!is.na(aot), !is.na(interp))

if (nrow(counts_wide) == 0) {
  stop("No paired aot/interp fault counts found")
}

# Compute correlation
cor_raw <- cor(counts_wide$aot, counts_wide$interp, method = "pearson")
cor_log <- cor(
  log10(counts_wide$aot),
  log10(counts_wide$interp),
  method = "pearson"
)
cat(sprintf("Pearson correlation (raw):   %.4f\n", cor_raw))
cat(sprintf("Pearson correlation (log10): %.4f\n", cor_log))

# Create plot
plot <- ggplot(
  counts_wide,
  aes(x = aot, y = interp, color = base_name, shape = resulttype)
) +
  # geom_abline(
  #   slope = 1,
  #   intercept = 0,
  #   colour = "grey70",
  #   linetype = "dotted"
  # ) +
  geom_point(size = 3, alpha = 0.7) +
  scale_x_log10(name = "AOT Fault Count") +
  scale_y_log10(name = "Interpreter Fault Count") +
  labs(
    # title = sprintf(
    #   "Fault Count Correlation (r_raw = %.4f, r_log = %.4f)",
    #   cor_raw,
    #   cor_log
    # ),
    title = "Fault Count Correlation",
    color = "Experiment",
    shape = "Fault Type"
  ) +
  theme_minimal() +
  theme(
    legend.position = "right",
    plot.title = element_text(size = 14, face = "bold")
  )

suffix <- gsub("^resultsdata|\\.csv$", "", csv_suffix)
outfile <- paste0("injections/fault_count_correlation", suffix, ".svg")
ggsave(outfile, plot = plot, width = 10, height = 8)
print(paste("Saved", outfile))
