## create_DT_expanded_with_NA_fillers.r

create_DT_expanded_with_NA_fillers <- function(DT="DT.split_percs.unexpanded") {
    ## Using:   kCols.splitgroup_nodate  for determining the splits, then date for chopping them up
    
    if (is.character(DT))
        DT <- get(DT)

    temp_DT.splitCols_expanded <- DT[, list(subtotal_nodate=sum(OA_gross_forDSOM)), keyby=kCols.splitgroup_nodate]

    ## -------------------------------------------------------------------------------- ##
    ## Add in default values
    ## -------------------------------------------------------------------------------- ##
    ## Add in every SC to every store, using list(label_sc_group=SCs) over kCols.store
    ## Then merge it back into the original temp_DT.splitCols_expanded
    ## and set the NAs in subtotal_nodate to $0 and the meta values to default
    ## CONFIRM FIRST that there are NO NAs in anypart of temp_DT.splitCols_expanded
    stopifnot(!is.na(temp_DT.splitCols_expanded[, c("subtotal_nodate", cols.to.spliton), with=FALSE]))
    setkeyIfNot(temp_DT.splitCols_expanded, kCols.storeSC, superset.ok=TRUE, verbose=FALSE)
    temp_DT.splitCols_expanded <- temp_DT.splitCols_expanded[setkey(temp_DT.splitCols_expanded[, list(label_sc_group=oscFactorLevels), by=kCols.store]), allow=TRUE]
    temp_DT.splitCols_expanded[is.na(subtotal_nodate), c(names(default.metavalues), "subtotal_nodate") := c(default.metavalues, 0)]

    ## CONFIRM: If everything was done right, then the sum in temp_DT.splitCols_expanded should be equal to that of DT
    stopifnot(equals(sumn(temp_DT.splitCols_expanded$subtotal_nodate), sumn(DT$OA_gross_forDSOM)))

    ## CONFIRM AGAIN, No NAs in temp_DT.splitCols_expanded
    stopifnot(!is.na(temp_DT.splitCols_expanded))
    ## -------------------------------------------------------------------------------- ##

    browser(expr=inDebugMode("NA Filler"), text="In function create_DT_expanded_with_NA_fillers.\nCreated temp_DT.splitCols_expanded but did not create the column of NA filler yet")

    ## -------------------------------------------------------------------------------- ##
    ## WE WILL USE THE AVG AS THE NA FILLER -- FOR NOW
    ## -------------------------------------------------------------------------------- ##

    ## Calculate the avg percent of a store's total revenue. 
    temp_DT.splitCols_expanded [, avg_percentOfStoreTotal_bySCMeta := subtotal_nodate / sum(subtotal_nodate), by=kCols.store] 

    ## Confirms that the percentages to to one 
    sumsToOne(temp_DT.splitCols_expanded, "avg_percentOfStoreTotal_bySCMeta", by=kCols.store)

    ## We do not need  avg_percentOfStoreTotal_bySC but if we did, it would be calculated as such
    {
      temp_DT.splitCols_expanded [, storeTotal                   := sum(subtotal_nodate),            by=kCols.store]
      temp_DT.splitCols_expanded [, avg_percentOfStoreTotal_bySC := sum(subtotal_nodate)/storeTotal, by=kCols.storeSC] 
      sumsToOne(temp_DT.splitCols_expanded[, unique(avg_percentOfStoreTotal_bySC), by=kCols.storeSC], "V1", by=kCols.store)
      temp_DT.splitCols_expanded [, storeTotal := NULL]
    }

    ## avg_percentOfStoreTotal_byMeta
    {
      temp_DT.splitCols_expanded [, storeTotal                   := sum(subtotal_nodate),            by=kCols.store]
      temp_DT.splitCols_expanded [, avg_percentOfStoreTotal_byMeta := sum(subtotal_nodate)/storeTotal, by=c(kCols.store, cols.to.spliton)] 
      sumsToOne(temp_DT.splitCols_expanded[, unique(avg_percentOfStoreTotal_byMeta), by=c(kCols.store, cols.to.spliton)], "V1", by=kCols.store)
      temp_DT.splitCols_expanded [, storeTotal := NULL]
    }

    ## TEST
    {
      by.thisGroup <- c(kCols.store, "label_sc_group", cols.to.spliton)
      nm.thisGroup <- "Meta"

      newCol <- sprintf("avg_percentOfStoreTotal_by%s", "TEST")
      temp_DT.splitCols_expanded [, storeTotal                   := sum(subtotal_nodate),            by=kCols.store]
      temp_DT.splitCols_expanded [, (newCol) := sum(subtotal_nodate)/storeTotal, by=by.thisGroup] 
      sumsToOne(temp_DT.splitCols_expanded[, unique(get(newCol)), by=by.thisGroup], "V1", by=kCols.store)
      ## Cleanup
      temp_DT.splitCols_expanded [, storeTotal := NULL]
      rm(by.thisGroup, nm.thisGroup)
    }



    ## we do not need the subtotal value  
    temp_DT.splitCols_expanded[, subtotal_nodate := NULL]
    ## -------------------------------------------------------------------------------- ##

    setnames(temp_DT.splitCols_expanded, "avg_percentOfStoreTotal_bySCMeta", ".perc_split_NA_filler.")

    setkeyIfNot(temp_DT.splitCols_expanded, kCols.splitgroup_nodate, verbose=FALSE)
    return(temp_DT.splitCols_expanded)
}


