source("/home/ion606/Desktop/Homework/Data Analytics/Assignments/Assignment II/R/00_utils.R")
ctx <- jsonlite::fromJSON("/home/ion606/Desktop/Homework/Data Analytics/Assignments/Assignment II/output/ctx.json")
df <- suppressMessages(readr::read_csv(ctx$data, show_col_types = FALSE))

fit_ols <- function(data, y_col, x_cols, name_tag, fig_dir, stats_dir) {
	d <- data |> dplyr::select(all_of(c(y_col, x_cols))) |> tidyr::drop_na()
	for (xc in x_cols) d[[xc]] <- tf_pos(d[[xc]])
	f <- as.formula(paste(y_col, "~", paste(x_cols, collapse = " + ")))
	m <- lm(f, data = d)

	# residuals vs fitted
	res_path <- file.path(fig_dir, paste0("residuals_", sanitize(name_tag), ".png"))
	p_res <- tibble(fitted = fitted(m), resid = resid(m)) |>
		ggplot(aes(fitted, resid)) +
		geom_point(size = 1.6) +
		geom_hline(yintercept = 0) +
		labs(title = paste("residuals vs fitted:", name_tag),
			x = "fitted", y = "residuals") +
		theme_minimal()
	save_plot(p_res, res_path)

	# scatter first predictor vs response
	first <- x_cols[1]
	sc_path <- file.path(fig_dir, paste0("scatter_", sanitize(name_tag), "_", sanitize(first), ".png"))
	p_sc <- d |>
		ggplot(aes(.data[[first]], .data[[y_col]])) +
		geom_point(size = 1.6) +
		labs(title = paste(first, "vs", y_col), x = first, y = y_col) +
		theme_minimal()
	save_plot(p_sc, sc_path)

	# summary to text
	summ_path <- file.path(stats_dir, paste0("ols_", sanitize(name_tag), ".txt"))
	capture.output(summary(m), file = summ_path)

	gl <- broom::glance(m)
	list(
		name = name_tag,
		rsq = unname(gl$r.squared),
		aic = unname(gl$AIC),
		bic = unname(gl$BIC),
		nobs = stats::nobs(m),
		summary_file = summ_path,
		residuals_fig = res_path,
		scatter_fig = sc_path
	)
}

ols <- list()
if (length(ctx$predictors)) {
	for (p in ctx$predictors) {
		tag <- paste0("full: ", ctx$response, " ~ ", paste(p, collapse = " + "))
		ols <- append(ols, list(fit_ols(df, ctx$response, p, tag, ctx$fig_dir, ctx$stats_dir)))
	}
}

ctx$ols <- ols
writeLines(jsonlite::toJSON(ctx, pretty = TRUE, auto_unbox = TRUE), "/home/ion606/Desktop/Homework/Data Analytics/Assignments/Assignment II/output/ctx.json")
message("ols (full) done")
