library(tidyverse)
count_data_fp <-"/mmfs1/gscratch/comdata/users/mjilg/mw-repo-lifecycles/case1/event_0430_framework_commit_counts.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")

summed_df <- long_df |>
  group_by(relative_week, project, commit_type) |>
  summarize(total_commits = sum(lengthened_commit_count), .groups = 'drop')

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

commit_authors <- summed_df |>
  ggplot(aes(x=relative_week, 
             y=total_commits, 
             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("Weekly Commits to Bot Frameworks During VE Deployment") +
  theme_bw() + 
  theme(legend.position = "top") 
commit_authors

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


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(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 Bot Framework Commits Surrounding VE Deployment") +
  theme_bw() +
  theme(legend.position = "top")

new_unaff_authors

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