library(tidyverse)
count_data_fp <-"/mmfs1/gscratch/comdata/users/mjilg/mw-repo-lifecycles/case2/event_0430_mediawiki_core_weekly_count.csv"
input_df <- read.csv(count_data_fp, header = TRUE) 

input_df$nonbot_commit_count <- input_df$commit_count - input_df$bot_commit_count
input_df$unaff_commit_count <- input_df$nonbot_commit_count - input_df$wikimedia_commit_count

library(scales)
library(ggplot2)

long_df <- input_df |>
  tidyr::pivot_longer(cols = c(unaff_commit_count, wikimedia_commit_count),
                      names_to = "commit_type",
                      values_to = "lengthened_commit_count")

affiliationColors <-
  setNames( c('#5da2d8', '#c7756a')
            ,c("unaff_commit_count", "wikimedia_commit_count"))

commit_authors <- long_df |>
  ggplot(aes(x=relative_week, 
             y=lengthened_commit_count, 
             fill=factor(commit_type))) +
  geom_col(position='dodge') +
  labs(x = "Relative Week", y = "Commits", fill="Commit Type") +
  scale_fill_manual(values = affiliationColors,
                     labels = c("unaff_commit_count" = "Unaffiliated Commits",
                                "wikimedia_commit_count" = "WMF Commits")) +
  ggtitle("Total Relevant Core Commits Around HTTPS-as-default Deployment") +
  theme_bw() + 
  theme(legend.position = "top")
commit_authors

ggsave(filename = "ww-c2-0430-commits.png", plot = commit_authors, width = 12, height = 9, dpi = 800)


# new affiliation things 
# relative week >= -4 
# new unaffiliated 
# returning unaffiliated 
input_df$returning_unaff_commit_count = input_df$unaff_commit_count - input_df$unaff_new_commit_count


new_authors_long_df <- input_df |>
  filter(relative_week >= -4) |>
  tidyr::pivot_longer(cols = c(unaff_new_commit_count, returning_unaff_commit_count),
                      names_to = "commit_seniority",
                      values_to = "lengthened_commit_count")

new_unaff_authors <- new_authors_long_df |>
  ggplot(aes(x=relative_week,
             y=lengthened_commit_count, 
             fill=commit_seniority)) +
  geom_col(color = '#5da2d8', position='dodge') +
  labs(x = "Relative Week", y = "Commits", fill="Commit Seniority ('New' contributors <= 5 commits before 06-06-2013)") +
  scale_fill_manual(values = c("returning_unaff_commit_count" = "#FFC107",  # Color for "Returning Contributors"
                               "unaff_new_commit_count" = "#004D40"),   
    labels = c("returning_unaff_commit_count" = "Returning Contributors",
               "unaff_new_commit_count" = "New Contributors")
  ) +
  ggtitle("Unaffiliated Relevant Core Commits Around HTTPS-as-default Deployment") +
  theme_bw() +
  theme(legend.position = "top")

new_unaff_authors

ggsave(filename = "ww-c2-0430-unaff-commit-spike.png", plot = new_unaff_authors, width = 12, height = 9, dpi = 800)

new_authors <- long_df |>
  ggplot(aes(x=relative_week, 
             y=lengthened_commit_count, 
             color=factor(commit_type))) +
  geom_point() + 
  geom_line() +
  labs(x = "Relative Week", y = "Commits", color="Commit Type") +
  scale_color_manual(values = affiliationColors,
                     labels = c("nonbot_commit_count" = "Total Nonbot Commits",
                                "unaff_new_commit_count" = "New Unaffiliated Commits",
                                "wmf_new_commit_count" = "New WMF Commits")) +
  ggtitle("Total MW-core Commits Around HTTP-deprecation ('New' contributors <= 5 commits 04-01-2015 and 06-01-2015)") +
  theme_bw() 
  #+ 
  #theme(legend.position = "top")
new_authors

ggsave(filename = "0403-https-core-event-new-commits.png", plot = new_authors, width = 12, height = 9, dpi = 800)


window_num <- 12
intermediate_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) |>
  filter(relative_week >= (- window_num) & relative_week <= (window_num)) 

time_plot <- intermediate_df |>
  ggplot(aes(x=relative_week, y=nonbot_commit_count)) +
  labs(x="Weekly Offset", y="Nonbot Commit Count") +
  geom_smooth() +
  geom_vline(xintercept = 0)+
  theme_bw() +
  theme(legend.position = "top")
time_plot

library(dplyr)

share_df <- intermediate_df |>
  mutate(wikimedia_share = wikimedia_commit_count / nonbot_commit_count) |>
  mutate(other_share = other_commit_count / nonbot_commit_count)|>
  drop_na()

share_long <- share_df |>
  dplyr::select(relative_week, wikimedia_share, other_share) |>
  pivot_longer(cols = c(wikimedia_share, other_share), names_to = "category", values_to = "share")

commit_share_plot <- share_long |>
  ggplot(aes(x=relative_week, 
             y=share, 
             color=category)) +
  geom_line() +
  geom_point() + 
  labs(x = "Relative Week", y = "Share of Nonbot Commits", color="Commit Author Affiliation") +
  scale_color_discrete(labels = c("Unaffiliated", "Organizationally Affiliated")) +
  ggtitle("MW-core Nonbot Total Commit Share Around HTTP-deprecation") +
  theme_bw() + 
  theme(legend.position = "top")
commit_share_plot
