#trying to make a time plot showing the over-time shift
library(plyr) 
contrib_df <- read_csv('../final_data/deb_contrib_did.csv')
readme_df <- read_csv("../final_data/deb_readme_did.csv")
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")
#first contrib
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)]
#then readme
readme_df <- readme_df[,col_order]
readme_df$ct_before_all <- str_split(gsub("[][]","", readme_df$before_all_ct), ", ")
readme_df$ct_after_all <- str_split(gsub("[][]","", readme_df$after_all_ct), ", ")
readme_df$ct_before_mrg <- str_split(gsub("[][]","", readme_df$before_mrg_ct), ", ")
readme_df$ct_after_mrg <- str_split(gsub("[][]","", readme_df$after_mrg_ct), ", ")
drop <- c("before_all_ct", "before_mrg_ct", "after_all_ct", "after_mrg_ct")
readme_df = readme_df[,!(names(readme_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_contrib_data <- expand_timeseries(contrib_df[1,])
for (i in 2:nrow(contrib_df)){
  expanded_contrib_data <- rbind(expanded_contrib_data, expand_timeseries(contrib_df[i,]))
}
expanded_readme_data <- expand_timeseries(readme_df[1,])
for (i in 2:nrow(readme_df)){
  expanded_readme_data <- rbind(expanded_readme_data, expand_timeseries(readme_df[i,]))
}
window_num <- 8
windowed_contrib_data <- expanded_contrib_data |>
  filter(week >= (27 - window_num) & week <= (27 + window_num)) |>
  mutate(D = ifelse(week > 27, 1, 0))
windowed_readme_data <- expanded_readme_data |>
  filter(week >= (27 - window_num) & week <= (27 + window_num)) |>
  mutate(D = ifelse(week > 27, 1, 0))

windowed_contrib_data$week_offset <- windowed_contrib_data$week - 27
all_actions_contrib_data <- windowed_contrib_data[which(windowed_contrib_data$observation_type == "all"),]
all_actions_contrib_data$document_type <- rep("CONTRIBUTING", length(all_actions_contrib_data$count))
windowed_readme_data$week_offset <- windowed_readme_data$week - 27
all_actions_readme_data <- windowed_readme_data[which(windowed_readme_data$observation_type == "all"),]
all_actions_readme_data$document_type <- rep("README", length(all_actions_readme_data$count))
all_actions_data <- rbind(all_actions_contrib_data, all_actions_readme_data)
all_actions_data$log1p_count <- log1p(all_actions_data$count)
library(scales)
expm1_trans <- trans_new(
  name = 'expm1',
  transform = function(x) expm1(x),
  inverse = function(x) log1p(x)
)

doctypeColors <-
  setNames( c('#5da2d8', '#c7756a')
            , c("CONTRIBUTING", "README"))

time_plot <- all_actions_data |>
  ggplot(aes(x=week_offset, y=count, color=factor(document_type))) +
  scale_y_continuous(trans = 'log1p', labels = scales::comma) +
  labs(x="Weekly Offset", y="Commit Count", color="Document Type: ") +
  scale_color_manual(values = doctypeColors) + 
  geom_smooth() +
  geom_vline(xintercept = 0)+
  theme_bw() +
  theme(legend.position = "top")
time_plot

ggsave(filename = "012825_gam_introduction.png", plot = time_plot, width = 8, height = 6, dpi = 500)
#code to change the axes

#scale_y_continuous(breaks = c(0, 0.5, 1.0, 1.5), 
#                   labels = round(c(expm1(0), expm1(0.5), expm1(1.0), expm1(1.5)), 1)) +

#looking at event gap 
mean(all_actions_readme_data$event_gap)
sd(all_actions_readme_data$event_gap)
mean(all_actions_contrib_data$event_gap)
sd(all_actions_contrib_data$event_gap)

#all_actions_contrib_data$log1p_count <- log1p(all_actions_contrib_data$count)
#contrib_time_plot <- all_actions_contrib_data |>
#  ggplot(aes(x=week_offset, y=log1p_count)) + 
#  geom_smooth(color=forestgreen) +
#  geom_vline(xintercept = 0)+
#  annotate("text", x=3, y=1, label="CONTRIBUTING.md Publication", angle=0)+
#  theme_bw() +
#  ylab("Log Transformed Count of Contributions") +
#  xlab("Offset Weeks") +
#  theme(legend.position = "top")
#contrib_time_plot

