# Five invented people, in months from a common time origin.
# status: 1 = first event; 0 = right censoring without an observed event.
# Educational Kaplan-Meier example only; these are not TALAPRO-3 data.
# Run: Rscript synthetic-km.R output-directory
args <- commandArgs(trailingOnly = TRUE)
out <- if (length(args)) args[[1]] else "."
dir.create(out, recursive = TRUE, showWarnings = FALSE)
data <- data.frame(synthetic_id = 1:5, month = c(2, 4, 6, 8, 10),
                   status = c(1, 0, 1, 0, 0))
write.csv(data, file.path(out, "synthetic-km.csv"), row.names = FALSE)
fit <- survival::survfit(survival::Surv(month, status) ~ 1, data = data)
times <- c(0, 2, 4, 6, 8, 10)
risk <- summary(fit, times = times)$n.risk
labels <- list(
  es = list(y = "Probabilidad sin evento", x = "Meses desde el inicio",
            curve = "Kaplan-Meier", censor = "Censura", threshold = "Umbral de mediana: 50%",
            risk = "En riesgo justo antes del mes indicado", final = "53,3%"),
  en = list(y = "Event-free probability", x = "Months from time origin",
            curve = "Kaplan-Meier", censor = "Censoring", threshold = "Median threshold: 50%",
            risk = "At risk just before the stated month", final = "53.3%")
)
for (lang in names(labels)) {
  txt <- labels[[lang]]
  for (size in c("desktop", "mobile")) {
    mobile <- size == "mobile"
    filename <- paste0("km-synthetic-", lang, "-", size, "-20261029.png")
    png(file.path(out, filename), width = if (mobile) 800 else 1440,
        height = if (mobile) 1000 else 850, res = 160,
        pointsize = if (mobile) 17 else 15, family = "Arial", type = "cairo", bg = "white")
    layout(matrix(1:2, ncol = 1), heights = c(4.6, 1.05))
    par(mar = c(3.9, 4.4, 1.0, 1.0), fg = "#17384C", col.axis = "#17384C",
        col.lab = "#17384C", las = 1, mgp = c(2.8, 0.7, 0))
    plot(NA, xlim = c(0, 10.6), ylim = c(0, 1.03), xaxs = "i", yaxs = "i",
         axes = FALSE, xlab = "", ylab = "")
    abline(h = c(0.25, 0.75, 1), col = "#E2E9EC", lwd = 0.8)
    abline(h = 0.5, col = "#667B89", lty = 2, lwd = 1.4)
    lines(fit, conf.int = FALSE, mark.time = TRUE, pch = 3,
          col = "#087E8B", lwd = 3, cex = 1.15)
    axis(1, at = times, cex.axis = 0.85, tcl = -0.3)
    axis(2, at = seq(0, 1, 0.25), labels = paste0(seq(0, 100, 25), "%"),
         cex.axis = 0.85, tcl = -0.3)
    mtext(txt$x, side = 1, line = 2.5, cex = 0.88)
    mtext(txt$y, side = 2, line = 3.25, las = 0, cex = 0.88)
    legend("bottomleft", inset = c(0.02, 0.02),
           legend = c(txt$curve, txt$censor, txt$threshold),
           col = c("#087E8B", "#087E8B", "#667B89"),
           lty = c(1, NA, 2), pch = c(NA, 3, NA), lwd = c(3, NA, 1.4),
           cex = if (mobile) 0.73 else 0.85, bty = "n", y.intersp = 1.15)
    text(9.9, 0.63, txt$final, adj = 1, cex = 0.95, font = 2, col = "#087E8B")
    par(mar = c(0.7, 4.4, 2.1, 1.0))
    plot.new()
    plot.window(xlim = c(0, 10.6), ylim = c(0, 1), xaxs = "i", yaxs = "i")
    mtext(txt$risk, side = 3, line = 0.7, adj = 0,
          cex = if (mobile) 0.67 else 0.82, font = 2)
    text(times, 0.55, risk, cex = 0.95, xpd = NA)
    dev.off()
  }
}
writeLines(c(paste("R", getRversion()), paste("survival", packageVersion("survival")),
             "S(2) = 4/5; S(6) = (4/5)*(2/3) = 8/15; median = NA",
             "Risk counts are immediately before each indicated time; no confidence intervals shown."),
           file.path(out, "figure-validation.txt"))
