library(tidyverse)
library(purrr)
library(readr)
library(stringr)
library(lubridate)
library(tidyr)

data_dir = "/gscratch/comdata/users/mjilg/mw-repo-lifecycles/commit_data/bot_frameworks"

csv_files <- list.files(data_dir, pattern = "*.csv", full.names = TRUE)

read_and_label <- function(file) {
  project_name <- basename(file) %>% 
    stringr::str_remove("_commits.csv")
  read_csv(file) %>% 
    mutate(project = project_name)
}

all_data <- csv_files %>% 
  map_df(read_and_label)

# TODO: this is project/event specific
#event_date <- as.Date("2013-07-01")
#event_date <- as.Date("2013-08-28")
event_date <- as.Date("2015-07-02")

df <- all_data |>
  mutate(commit_date = ymd_hms(commit_date))

df <- df %>%
  group_by(project) %>%
  mutate(oldest_commit_date = min(as.Date(commit_date))) %>%
  ungroup() %>%
  mutate(age = as.numeric(as.Date("2025-02-10") - oldest_commit_date))

#discard projects who are created after the focal event date
df <- df %>%
  group_by(project) %>%
  filter(min(as.Date(commit_date)) <= event_date) %>%
  ungroup()

#calculated_start_date <- event_date %m-% months(12)
#%start_date <- max(calculated_start_date, df$oldest_commit_date)
#end_date <- event_date %m+% months(12)

#getting the relative weeks to the publication date
relative_week <- function(date, ref_date) {
  as.integer(as.numeric(difftime(date, ref_date, units = "days")) %/% 7)
}

old_author_list <- df |>
  filter(commit_date > as.Date("2013-07-01") & commit_date < as.Date("2015-06-12"))|>
  group_by(author_email) |>
  summarise(commit_count = n()) |>
  filter(commit_count > 5) |>
  pull(author_email)

df <- df |>
  mutate(new_author = ifelse(author_email %in% old_author_list, 0, 1), 
         new_author_wmf = if_else(grepl("@wikimedia", author_email), 
                                  new_author, 0),
         new_author_unaff = if_else(!grepl("@wikimedia", author_email) &
                                      !grepl("l10n-bot@translatewiki.net|tools.libraryupgrader@tools.wmflabs.org", author_email),
                                    new_author, 0))

filtered_df <- df |>
  mutate(relative_week = relative_week(commit_date, event_date)) |>
  arrange(relative_week) |>
  filter(relative_week >= (-104) & relative_week <= 13) |>
  ungroup()


weekly_commits <- filtered_df |>
  group_by(project, relative_week, age) |>
  summarise(commit_count = n(),
            author_emails = list(unique(author_email)),
            committer_emails = list(unique(committer_email)),
            mediawiki_dev_commit_count = sum(grepl("@users.mediawiki.org", author_email)),
            wikimedia_commit_count = sum(grepl("@wikimedia", author_email)),
            wikia_commit_count = sum(grepl("@wikia-inc.com", author_email)),
            bot_commit_count = sum(grepl("l10n-bot@translatewiki.net|tools.libraryupgrader@tools.wmflabs.org", author_email)),
            wmf_new_commit_count = sum(new_author_wmf),
            unaff_new_commit_count = sum(new_author_unaff),
            .groups = 'drop') |>
  replace_na(list(commit_count = 0)) |>
  replace_na(list(wikimedia_commit_count = 0)) |>
  replace_na(list(l10n_commit_count = 0)) |>
  replace_na(list(jenkins_commit_count = 0)) |>
  replace_na(list(mediawiki_dev_commit_count = 0)) |>
  replace_na(list(wikia_commit_count = 0)) |>
  replace_na(list(wmf_ft_commit_count = 0)) |>
  replace_na(list(unaff_ft_commit_count = 0)) |>
  mutate(before_after = if_else(relative_week < 0, 0, 1)) |>
  select(-author_emails, -committer_emails)


#weekly_commits <- weekly_commits |>
#  filter(relative_week >= (-52) & relative_week <= 52 )

weekly_commits

output_filepath <-"/mmfs1/gscratch/comdata/users/mjilg/mw-repo-lifecycles/case3/event_0430_framework_commit_counts.csv"
write.csv(weekly_commits, output_filepath, row.names = FALSE)