library(tidyverse)
entest_fp <-"/mmfs1/gscratch/comdata/users/mjilg/mw-repo-lifecycles/case1/en-testing_0314_bot_frameworks_weekly_commit_count_data.csv"
entest_df <- read.csv(entest_fp, header = TRUE) |> mutate(rd_event = "en-testing")

widetest_fp <-"/mmfs1/gscratch/comdata/users/mjilg/mw-repo-lifecycles/case1/wide-testing_0314_bot_frameworks_weekly_commit_count_data.csv"
widetest_df <- read.csv(widetest_fp, header = TRUE) |> mutate(rd_event = "wide-testing")

event_fp <-"/mmfs1/gscratch/comdata/users/mjilg/mw-repo-lifecycles/case1/event_0314_bot_frameworks_weekly_commit_count_data.csv"
event_df <- read.csv(event_fp, header = TRUE) |> mutate(rd_event = "default")

#input_df <- bind_rows(entest_df, widetest_df, event_df)
#dropping the event (2013-07-01) from the modeling
#input_df <- bind_rows(entest_df, widetest_df)
input_df <- event_df

input_df <- input_df |>
  mutate(nonbot_commit_count = commit_count - bot_commit_count)|>
  mutate(other_commit_count = nonbot_commit_count - mediawiki_dev_commit_count - wikimedia_commit_count) |>
  mutate(wikimedia_commit_count = wikimedia_commit_count + mediawiki_dev_commit_count) |>
  dplyr::select(-mediawiki_dev_commit_count) |>
  dplyr::select(-wikia_commit_count)

#get into mlm format
long_df <- input_df |>
  tidyr::pivot_longer(cols = c(nonbot_commit_count, other_commit_count, wikimedia_commit_count),
               names_to = "commit_type",
               values_to = "lengthened_commit_count")

summarized_df <- long_df |>
  group_by(relative_week, commit_type, rd_event) |>
  summarise(total_commit_count = sum(lengthened_commit_count, na.rm = TRUE)) 
#long_df <- input_df |>
#  tidyr::pivot_longer(cols = c(wmf_ft_commit_count, unaff_ft_commit_count, nonbot_commit_count),
#               names_to = "commit_type",
#               values_to = "lengthened_commit_count")

#intermediate_long_df <- long_df |>
#  mutate(commit_share = lengthened_commit_count / (nonbot_commit_count)) |>
#  mutate(log_commits = log1p(lengthened_commit_count))|>
#  mutate(scaled_long_commits = lengthened_commit_count / 10) 


window_num <- 8
final_long_df <- summarized_df |>
  filter(relative_week >= (- window_num) & relative_week <= (window_num)) 

affiliationColors <-
  setNames( c('black','#5da2d8', '#c7756a')
            ,c("nonbot_commit_count","other_commit_count", "wikimedia_commit_count"))

#  linetype = rd_event

commit_plot <- final_long_df |>
  ggplot(aes(x=relative_week, 
              y=total_commit_count, 
              color=factor(commit_type))) +
  geom_line() +
  geom_point() + 
  labs(x = "Relative Week", y = "Commits", linetype = "Testing Event", color="Commit Author Affiliation") +
  scale_color_manual(values = affiliationColors,
                     labels = c("other_commit_count" = "Unaffiliated", "wikimedia_commit_count" = "WMF Affiliated")) + 
#  scale_linetype_discrete(labels = c("enwiki testing (2012-12-11)", "wide testing (2013-04-25)")) + 
  ggtitle("bot-frameworks Commits Around Wide Release (by Affiliation)") +
  theme_bw() + 
  theme(legend.position = "top")
commit_plot

ggsave(filename = "0401-bot-frameworks-commits-event.png", plot = commit_plot, width = 15, height = 9, dpi = 800)

total_commit_plot <- final_long_df |>
  ggplot(aes(x=relative_week, 
             y=nonbot_commit_count, 
             linetype = rd_event)) +
  geom_line() +
  geom_point() + 
  geom_vline(xintercept = 0, linetype = "dotted", color = "black") +
  labs(x = "Relative Week", y = "Nonbot Commit Count", linetype = "Testing Event") +
  scale_linetype_discrete(labels = c("enwiki testing (2012-12-12)", "wide testing (2013-04-25)")) + 
  ggtitle("VisualEditor Weekly Nonbot Commits Surrounding Opt-In Testing Events") +
  theme_bw() + 
  theme(legend.position = "top")
total_commit_plot

ggsave(filename = "0305-ve-total-commits.png", plot = total_commit_plot, width = 15, height = 9, dpi = 800)



commit_share_plot <- final_long_df |>
  ggplot(aes(x=relative_week, 
             y=commit_share, 
             color=commit_type,
             linetype = rd_event)) +
  geom_line() +
  geom_point() + 
  labs(x = "Relative Week", y = "Share of Nonbot Commits", linetype = "Testing Event", color="Commit Author Affiliation") +
  scale_color_manual(values = affiliationColors,
                     labels = c("other_commit_count" = "Unaffiliated", "wikimedia_commit_count" = "WMF Affiliated")) + 
  scale_linetype_discrete(labels = c("enwiki testing (2012-12-12)", "wide testing (2013-04-25)")) + 
  ggtitle("Share of Weekly VisualEditor Commits Surrounding Opt-in Testing Events") +
  theme_bw() + 
  theme(legend.position = "top")
commit_share_plot

ggsave(filename = "0312-ve-ve-testing-share.png", plot = commit_share_plot, width = 12, height = 9, dpi = 800)
