###   NOTE:   This file simply queries the OA DB and 
###             creates  DT.acc and DT.anal
###
###           There is no need to re-run this when Dev'ing          
###



## Which column to use as the music_vs_video split
col.music_vs_video <- "music_vs_video_by_product"  ## Other option is music_vs_video_by_transac  However, note that this latter option has a value 'Music OR Video'


## Dimensions common to accounting and analytics.  Avoids retyping and more clearly shows what is unique
sharedDims <- c("country_code", "storeid", store_name="store_name_full", "store_group", "store_musicbucket", "store_videobucket", "release_is_compilation", "release_product_type", "music_vs_video", "label_sc_group", "transac_type_abbr", "stream_vs_download") #  "music_vs_video_by_product", "music_vs_video_by_transac",  "transac_typeid", "transac_type_group", "transac_type", "store_group_less"

## Keys specific to OA DT tables
OA_kCols <- c("date", "storeid", "country_code", "release_is_compilation", "release_product_type", "label_sc_group", "transac_type_abbr", "stream_vs_download")

maxDate <- list()
maxDate[["accounting"]] <- {if (!RunIsAForecast()) NULL else findMaxDate("accounting", fakeToday)}
maxDate[["analytics"]]  <- {if (!RunIsAForecast()) NULL else findMaxDate("analytics",  fakeToday)}

### Arguments used for each
ARGS <- {
  list(
      ## ACCOUNTING 
      accounting=
      list(
            dimensions  =  c("accounting_month", "activity_month", "store_is_in_analytics"
                             , sharedDims) # , "music_vs_video_by_tracktype"
          , measures    =  c("gross", "units")
          , tbl         =  "accounting"
          , schema      =  "bi"
          , dateCol     =  "accounting_month"
          , dateCol.max =  "accounting_month"
          , maxDate     =  maxDate[["accounting"]]
        )
      , 

      ## ANALYTICS
      analytics = 
      list(
            dimensions  =  c("activity_month544", "paidunits_expected_ratio", sharedDims)
          , measures    =  c("paidunits", "freeunits", "gest", "gest_net_of_sc")
          , tbl         =  "analytics"
          , schema      =  "bi"
          , dateCol     =  "activity_month544"
          , dateCol.max =  "activity_date"
          , maxDate     =  maxDate[["analytics"]]
        )
  )
}

## Remaining arguments for createSummaryOfAggTables()
OtherArgs <- list(minDate = "2012-01-01", OA_kCols=OA_kCols, verbose.simple=TRUE, verbose=verbose.qry.oa, cluster=getOption("db.defaultcluster"))


{
  ## -------------------------------------------------------- ##
  ####         ------------------------------------         ####
  ####               Execute the Two Queries                ####
  ####         ------------------------------------         ####
  ## -------------------------------------------------------- ##
       if (verbose.showdbsettings) {
          message("Will run queries using:")
          showDBsettings()
          cat("\n")
       }

       Qres.list <- lapply(ARGS, function(X)
                        do.call(createSummaryOfAggTables, c(X, OtherArgs))     
                        )
  ## -------------------------------------------------------- ##


  ## -------------------------------------------------------- ##
  ##   confirm that keys are set and slight cleaning of NAs   ##
  ## -------------------------------------------------------- ##
    for (i in seq(Qres.list))
      setkeyIfNot(Qres.list[[i]], OA_kCols, organize=TRUE, verbose=FALSE)
  ## -------------------------------------------------------- ##

    ## New 20140915:
    DT.acc  <- changeAndAggregate(DT=Qres.list[["accounting"]], colsToAgg=ARGS[["accounting"]][["measures"]] , changeFunc="music_and_video_bucketcleanup_", exceptAllNA=TRUE)
    DT.anal <- changeAndAggregate(DT=Qres.list[["analytics"]],  colsToAgg=ARGS[["analytics"]][["measures"]]  , changeFunc="music_and_video_bucketcleanup_", exceptAllNA=TRUE)

    ## SINCE DT.anal includes fact_analytics_error, some transac_type_abbr & country_code will be missing. Fill those in with XXX
    DT.anal[transac_type_abbr == "", transac_type_abbr := "XXX"]
    DT.anal[country_code      == "", country_code      := "XXX"]

    ## CONFIRM: We expect zero NAs in the measure cols
    if (any(wh.anal <- DT.anal[transac_type_abbr != "XXX", sapply(.SD, function(x) any(is.na(x))), .SDcols=ARGS[["analytics"]][["measures"]]])) {
      warning (warningCols("There are NAs in DT.anal for columns ", nwhich(wh.anal)))
      print(unique(DT.anal[rowSums(is.na(DT.anal[, nwhich(wh.anal), with=FALSE])) >= 1 & transac_type_abbr != "XXX"], by=setdiff(OA_kCols, "date")))
    }
    if (any(wh.acc <- DT.acc[, sapply(.SD, function(x) any(is.na(x))), .SDcols=ARGS[["accounting"]][["measures"]]])) {
      warning (warningCols("There are NAs in DT.acc for columns ", nwhich(wh.acc)))
      print(DT.acc[rowSums(is.na(DT.acc[, nwhich(wh.acc), with=FALSE])) >= 1])
    }


    ## Add fakeToday attribute -- different from maxDate
    attr(DT.acc,  "fakeToday") <- fakeToday
    attr(DT.anal, "fakeToday") <- fakeToday
  ## ---------------------------------------------------------------- ##



  ## Save the query results, if using a fakeToday, rename it
  jesusForData_Forecasting(DT.anal)
  jesusForData_Forecasting(DT.acc)
}

