# preparing data for reporting propensity model
# runs quickly on laptop

rm(list=ls())
library(data.table)
library(Hmisc)

setwd('~/research/gTiger/Article_citsci/open_code')
source('settings/parameters.r')
source('settings/paths.r')

setwd(path_to_data)

mtable = readRDS('user_withdrawal_rates.Rds')

users = readRDS('user_registration_times.Rds')

user_participation_times = readRDS('user_participation_times.Rds')

rpd = readRDS('mosquito_alert_raw_reports.Rds')

names(rpd)[grep('version_UUID', names(rpd))] = 'version_UUID'

rpd = merge(rpd, users, by.x='user', by.y='user_UUID', all.x=T, all.y=F)

rpd$time_since_reg = difftime(rpd$ctime_posix, rpd$regtime_posix, units='days')

rpd = rpd[which(rpd$time_since_reg>0),]

rpd$days_since_reg = as.integer(rpd$time_since_reg)

rpd = data.table(rpd)

user_n_reports = rpd[ , list(n_reports = length(version_UUID)), by=user]

users = merge(users, user_n_reports, by.x='user_UUID', by.y='user', all.x=T, all.y=F)

users$n_reports[is.na(users$n_reports)] = 0

describe(users$n_reports)

users$ndays_last_report = unlist(lapply(1:nrow(users), function(i){
	this_user = users$user_UUID[i]
	these_rep_days_since_reg = rpd$days_since_reg[which(rpd$user==this_user)]
	if(length(these_rep_days_since_reg)>0){
	max(these_rep_days_since_reg)
	}
	else{
		return(0)
	}
}))

describe(users$ndays_last_report)

btimes = user_participation_times$total_participation_days

users$ndays = unlist(lapply(1:nrow(users), function(i){
	this_nday_last_report = users$ndays_last_report[i]
	these_btimes = btimes[which(btimes>=this_nday_last_report)] 
	this_sampled_ndays = these_btimes[sample(1:length(these_btimes), 1)]
	# here we are taking a random life time from the population of btimes that have already survived to this last report day
	ndays_toend = users$ndays_toend[i]
	return(min(ndays_toend, floor(this_sampled_ndays)))
}))

describe(users$ndays)

users$endtime_posix = users$regtime_posix + users$ndays*60*60*24

users$endtime_season_day = as.POSIXlt(users$endtime_posix)$yday

describe(users$regtime_season_day)

describe(users$endtime_season_day)

rcount = rpd[, list(n_reports=length(version_UUID)), by=list(user, days_since_reg)]

user_day_table = data.table(user = rep(users$user, 1+users$ndays), participation_days = unlist(lapply(1:nrow(users), function(i) 0:users$ndays[i])))

user_day_table = merge(user_day_table, rcount, by.x=c('user', 'participation_days'),by.y=c('user', 'days_since_reg'), all.x=T, all.y=T)

user_day_table = merge(user_day_table, users[, c('user_UUID', 'regtime_posix')], by.x='user',by.y='user_UUID', all.x=T, all.y=F)

user_day_table$date = user_day_table$participation_days*60*60*24 + user_day_table$regtime_posix

user_day_table$season_day = as.POSIXlt(user_day_table$date)$yday

user_day_table$study_day = floor(as.numeric(difftime(user_day_table$date, android_start_date, units="days")))

user_day_table$year = as.numeric(format(user_day_table$date, "%Y"))

user_day_table$year_week = paste0(format(user_day_table$date, "%Y"), '-', format(user_day_table$date, "%W"))

user_day_table$year_month = paste0(format(user_day_table$date, "%Y"), '-', format(user_day_table$date, "%m"))


user_day_table$any_reports = user_day_table$n_reports > 0

saveRDS(user_day_table, file='report_propensity.Rds')

saveRDS(users, file='user_reporting.Rds')
