# Rscript compare.R comparison.csv comparison.png
args <- commandArgs(trailingOnly = TRUE)
input <- if (length(args) >= 1) args[1] else "comparison.csv"
output <- if (length(args) >= 2) args[2] else "comparison.png"
d <- read.csv(input, fileEncoding = "UTF-8")

ink <- "#142D45"
muted <- "#455566"
teal <- "#00796A"
blue <- "#3355C2"

png(output, width = 1220, height = 860, res = 120, type = "cairo", bg = "#F4FAF8")
par(mar = rep(0, 4), xaxs = "i", yaxs = "i", family = "sans")
plot.new()
plot.window(c(0, 1220), c(860, 0))
lab <- function(x, y, value, size = 1, color = ink, bold = FALSE, align = 0) {
  text(x, y, value, col = color, cex = size, font = if (bold) 2 else 1, adj = c(align, 0.5))
}

rect(0, 0, 1220, 126, col = "#073936", border = NA)
segments(40, 774, 1180, 774, col = "#B7CFCC")
lab(40, 824, "FARLIGHT NEWS", .79, teal, TRUE)

if ("drug" %in% names(d)) {
  stopifnot(all(is.finite(d$percent_lower)), all(d$percent_lower >= 0 & d$percent_lower <= 100))
  d <- d[order(-d$percent_lower), ]
  lab(40, 45, "Compare the 13 negotiated Medicare prices", 1.6, "white", TRUE)
  lab(40, 88, "2025 Medicare gross cost and 2028 negotiated price per 30-day supply", .98, "white")
  lab(688, 156, "Percent lower", .86, muted, TRUE, 1)
  lab(885, 156, "2025 gross", .86, muted, TRUE, 1)
  lab(1156, 156, "2028 negotiated", .86, muted, TRUE, 1)
  prices <- function(value) paste0("$", format(value, big.mark = ",", trim = TRUE))
  y <- seq(197, 749, length.out = nrow(d))
  rect(40, y - 18, 1180, y + 18, col = rep(c("#E6F5EE", "#F4FAF8"), length.out = nrow(d)), border = NA)
  rect(260, y - 7, 610, y + 7, col = "#C8E7DE", border = NA)
  rect(260, y - 7, 260 + 350*d$percent_lower/100, y + 7, col = teal, border = NA)
  lab(55, y, d$drug, .95, ink, TRUE)
  lab(688, y, paste0(d$percent_lower, "%"), .9, teal, TRUE, 1)
  lab(885, y, prices(d$gross_2025_usd), .95, muted, align = 1)
  lab(1156, y, prices(d$negotiated_2028_usd), .95, ink, TRUE, 1)
  lab(40, 797, "CMS 2028 negotiated-price fact sheet. Patient out-of-pocket costs depend on coverage.", .78, muted)
} else if ("model" %in% names(d)) {
  lab(40, 45, "Compare Oregon’s five financing options", 1.6, "white", TRUE)
  lab(40, 88, "Projected 2032 revenue, in billions of dollars, and modeled household contributions", .98, "white")
  lab(40, 164, "Model", .82, muted, TRUE)
  lab(200, 164, "Payroll and household receipts", .82, muted, TRUE)
  lab(765, 164, "Total", .82, muted, TRUE, 1)
  lab(815, 164, "Tax returns owing no household contribution", .82, muted, TRUE)
  y <- seq(228, 694, length.out = nrow(d))
  fmt <- function(value) paste0("$", format(value, trim = TRUE), "B")
  rect(40, y - 47, 1180, y + 47, col = rep(c("#E6F5EE", "#F4FAF8"), length.out = nrow(d)), border = NA)
  lab(55, y, paste("Model", d$model), 1.05, ink, TRUE)
  rect(200, y - 22, 650, y - 8, col = "#D4E9E5", border = NA)
  rect(200, y + 12, 650, y + 26, col = "#DFE7F7", border = NA)
  rect(200, y - 22, 200 + 450*d$payroll_billions/35, y - 8, col = teal, border = NA)
  rect(200, y + 12, 200 + 450*d$household_billions/35, y + 26, col = blue, border = NA)
  lab(210 + 450*d$payroll_billions/35, y - 15, fmt(d$payroll_billions), .87, teal, TRUE)
  lab(210 + 450*d$household_billions/35, y + 19, fmt(d$household_billions), .87, blue, TRUE)
  lab(765, y, fmt(d$total_billions), .98, ink, TRUE, 1)
  rect(815, y - 7, 1040, y + 7, col = "#C8E7DE", border = NA)
  rect(815, y - 7, 815 + 225*d$returns_with_zero_household_contribution_pct/100, y + 7, col = teal, border = NA)
  lab(1156, y, paste0(d$returns_with_zero_household_contribution_pct, "%"), .98, teal, TRUE, 1)
  lab(200, 750, "Employer payroll tax", .82, teal, TRUE)
  lab(439, 750, "Household contribution", .82, blue, TRUE)
  lab(40, 797, "Oregon board report, Tables R3–R4. Model 1 uses the adjusted comparison rates.", .78, muted)
} else stop("Unsupported comparison dataset")

invisible(dev.off())
