setScience("AppleMusic", subProj="544Month")
wh <- getWH_already_on(default="science")

dbname <- "prod"
schema <- "bi"

## Main table
tbl.main <- "apple_544_lookup"
comment.main <- "Dates between 2011~2017 are accurate; Earlier dates simply use calendar month; later dates start to fall off due to leap years"

## Table with month ends
tbl.month_ends <- "apple_544_lookup_by_apple_month_ends"
comment.month_ends <- "Like apple_544_lookup, but using start and end of apple month; Useful for joining to reports provided from apple. If instead you want to join on calendar date, use apple_544_lookup"

minDate.using <- as.Date("2000-01-01")
maxDate.using <- as.Date("2030-01-01")
DT.itunes <- itunes_544(minDate=minDate.using, maxDate=maxDate.using) [, list(date=as.Date(unlist(dseq), origin=.origin)), by=month] %>% setkeyIfNot("date", organize=TRUE)
DT.itunes[, is_544 := TRUE]

if (minDate.using %ni% DT.itunes$date) {
    DT.rest <- seq.Date(from=as.Date("2000-01-01"), to=min(DT.itunes$date)-1, by="day") %>% data.table(date=.)
    DT.rest[, month := monthFloor(date)]
    DT.rest[, is_544 := FALSE]

    DT.itunes <- rbind(DT.itunes, DT.rest, use.names=TRUE)
    setkey(DT.itunes, "date")
}

setnames(DT.itunes, c("date", "month"), c("calendar_date", "apple_month"))

DT.itunes_month_ends <- DT.itunes[, list(apple_month_start = min(calendar_date), apple_month_end = max(calendar_date)), keyby=list(apple_month, is_544)]


setSnowflake(wh=wh)
ingestIntoSQL(DT=DT.itunes, schema=schema, tbl=tbl.main, dbname=dbname, comment_tbl=comment.main, drop=TRUE , datetime_type='TIMESTAMP_NTZ')
ingestIntoSQL(DT=DT.itunes_month_ends, schema=schema, tbl=tbl.month_ends, dbname=dbname, comment_tbl=comment.month_ends, drop=TRUE , datetime_type='TIMESTAMP_NTZ')

