library(here)
library(survival)
library(ggsurvfit)

source(here("code/helpers.R"))
options(arrow.skip_nul = TRUE)

a <- load_accounts() %>%
  filter(!has_moved) %>%
  filter(locked == FALSE) %>%
  anti_join(., arrow::read_feather(here("data/scratch/individual_moved_accounts.feather")), by=c("username"="moved_acct", "server"="moved_server")) %>%
  inner_join(arrow::read_feather(here("data/scratch/metadata.feather")), by="server") %>%
  filter(created_at > "2023-04-30") %>%
  filter(created_at <= "2023-06-30") %>%
  filter(created_at < last_status_at) %>%
  mutate(jm = server %in% arrow::read_feather(here("data/scratch/joinmastodon.feather"))$domain) %>%
  mutate(active = last_status_at >= as.Date("2023-12-01")) %>%
  #mutate(last_status_at = ifelse(active, lubridate::ymd_hms("2023-09-01 00:00:00", tz = "UTC"), last_status_at)) %>%
  mutate(active_time = difftime(ifelse(active, lubridate::ymd_hms("2023-12-01 00:00:00", tz = "UTC"), last_status_at), created_at, units="days")) %>%
  #mutate(active_time = difftime(last_status_at, created_at, units="days")) %>%
  mutate(status = ifelse(active, 0, 1))# %>% filter(followers_count > 0) %>% filter(following_count > 0)

server_summary <- a %>%
  group_by(server) %>%
  summarize(cohort_size = n(), .groups = "drop")

sel_a <- a %>%
  mutate(is_ms = server == "mastodon.social") %>%
  ungroup() %>%
  inner_join(server_summary, by = "server") %>% filter(!noindex) #%>% filter(user_count > 100)

cx <- sel_a %>%
  coxph(Surv(active_time, status) ~ is_ms + jm, data = ., x=TRUE, robust = T, cluster=server)

cz <- cox.zph(cx)

plot_survival <- sel_a %>%
  #filter(followers_count > 0) %>%
  #filter(following_count > 0) %>%
  mutate(id = paste(username, server, sep = "@")) %>%
  survfit2(
    Surv(active_time, status) ~ jm + is_ms, # is_jm
    data = ., id = id,
    cluster = server,
    robust = TRUE
  ) %>%
  ggsurvfit() +
  labs(
    y = "Overall survival probability",
    x = "Time (days)",
  ) +
  scale_fill_discrete(name = "Group", labels = c("Not in JM", "JM", "mastodon.social")) +
  scale_color_discrete(name = "Group", labels = c("Not in JM", "JM", "mastodon.social")) +
  theme_bw_small_labels() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.position="bottom")
