#libraries
library(readr)
library(tidyverse)
library(plyr)
contrib_df <- read_csv("../final_data/deb_contrib_did.csv")
contrib_pop_df <- read_csv("../final_data/deb_contrib_pop_change.csv")
contrib_readability_df <- read_csv('../text_analysis/dwo_readability_contributing.csv')
#get the contribution count 
#some preprocessing and expansion 
col_order <- c("upstream_vcs_link", "age_in_days", "first_commit", "first_commit_dt", "event_gap", "event_date",  "event_hash", "before_all_ct", "after_all_ct", "before_mrg_ct", "after_mrg_ct", "before_auth_new", "after_auth_new", "before_commit_new",  "after_commit_new")
contrib_df <- contrib_df[,col_order]
contrib_df$ct_before_all <- str_split(gsub("[][]","", contrib_df$before_all_ct), ", ")
contrib_df$ct_after_all <- str_split(gsub("[][]","", contrib_df$after_all_ct), ", ")
contrib_df$ct_before_mrg <- str_split(gsub("[][]","", contrib_df$before_mrg_ct), ", ")
contrib_df$ct_after_mrg <- str_split(gsub("[][]","", contrib_df$after_mrg_ct), ", ")
drop <- c("before_all_ct", "before_mrg_ct", "after_all_ct", "after_mrg_ct")
contrib_df = contrib_df[,!(names(contrib_df) %in% drop)]
# 2 some expansion needs to happens for each project 
expand_timeseries <- function(project_row) {
  longer <- project_row |> 
    pivot_longer(cols = starts_with("ct"),
                 names_to = "window",
                 values_to = "count") |>
    unnest(count) 
  longer$observation_type <- gsub("^.*_", "", longer$window)
  longer <- ddply(longer, "observation_type", transform, week=seq(from=0, by=1, length.out=length(observation_type)))
  longer$count <- as.numeric(longer$count)
  #longer <- longer[which(longer$observation_type == "all"),]
  return(longer)
}
expanded_data <- expand_timeseries(contrib_df[1,])
for (i in 2:nrow(contrib_df)){
  expanded_data <- rbind(expanded_data, expand_timeseries(contrib_df[i,]))
}
#filter out the windows of time that we're looking at
window_num <- 8
windowed_data <- expanded_data |>
  filter(week >= (27 - window_num) & week <= (27 + window_num)) |>
  mutate(D = ifelse(week > 27, 1, 0))

summed_data <- windowed_data |>
  filter(D==1) |>
  group_by(upstream_vcs_link) |>
  summarise_at(vars(count), list(summed_count=sum))

#concat dataframes into central data
contrib_pop_df <- contrib_pop_df |>
  mutate(project_name = map_chr(upstream_vcs_link, ~ {
    parts <- str_split(.x, pattern = "/")[[1]]
    if (length(parts) >= 1) {
      parts[length(parts)]  
    } else {
      NA_character_
    }
  }))

contrib_readability_df <- contrib_readability_df |>
  mutate(project_name = map_chr(filename, ~ {
    parts <- str_split(.x, pattern = "_")[[1]]
    if (length(parts) >= 1) {
      paste(head(parts, -1), collapse="_")
    } else {
      NA_character_
    }
  }))

contrib_total_df <- contrib_pop_df |>
  join(contrib_readability_df, by="project_name") 

contrib_total_df <- contrib_total_df|>
  join(summed_data, by="upstream_vcs_link")
#outcome variable that is number of commits by number of new contributors
contrib_total_df$commit_by_contrib = contrib_total_df$summed_count + contrib_total_df$after_contrib_new * 2
contrib_total_df$logged_outcome = log1p(contrib_total_df$commit_by_contrib)
contrib_total_df$logged_contribs = log1p(contrib_total_df$after_contrib_new)
contrib_total_df$logged_commits = log1p(contrib_total_df$summed_count)
# test regressions
library(MASS)
lm1 <- glm.nb(logged_contribs ~ reading_time + linsear_write_formula + flesch_reading_ease + mcalpine_eflaw + word_count, data = contrib_total_df)
qqnorm(residuals(lm1))
summary(lm1)
