# read the required packages
require(data.table)
require(lubridate)

# read the training set
xtrain <- fread('../input/train.csv')
date_train <- parse_date_time(xtrain$click_time, orders = 'YmdHMS')
# summary(date_train)
# train date ranges
# min: 2017-11-06 14:32:21 max: 2017-11-09 16:00:00

xtest <- fread('../input/test.csv')
date_test <- parse_date_time(xtest$click_time, orders = 'YmdHMS')
# summary(date_test)
# test date ranges
# min: 2017-11-10 04:00:00 max: 2017-11-10 15:00:00

# x0: 2017-11-06 14:32:21 -> 2017-11-08 16:00:00
# x1: 2017-11-09 04:00:00 -> 2017-11-09 15:00:00                       

# store training set
d1 <- parse_date_time('2017-11-08 16:00:00', orders = 'YmdHMS')
x0 <- xtrain[date_train <= d1]
write.csv(x0, '../input/x0.csv', row.names = F, quote = F)

# prepare evaluation set
d1 <- parse_date_time('2017-11-09 04:00:00', orders = 'YmdHMS')
d2 <- parse_date_time('2017-11-09 15:00:00', orders = 'YmdHMS')
x1 <- xtrain[date_train >= d1 & date_train <= d2]
x1$click_id <- 1:nrow(x1)

# by product: id + target columns for quick evaluation of predictions generated on x1
xlist <- c('click_id', 'is_attributed')
xref <- x1[,..xlist, with = TRUE ]
write.csv(xref, '../input/xref_x1.csv', row.names = F, quote = F)

x1[, attributed_time := NULL, with = TRUE]
x1[, is_attributed := NULL, with = TRUE]
write.csv(x1, '../input/x1.csv', row.names = F, quote = F)

