# screen -xRR Currency

setScience(proj="DeNormalizing", subProj="Currency_Neutral_Factors")
setGitBranchToSystem(); .g()

## warehouse to use
wh      <- sfWhicheverIsOn(whs=c("LOOKER_WH_ON", "SCIENCE", "LOAD_WH"))
schema  <- "BI"
tbl     <- "Currency_Neutral_Factors"

## File to ingest
f.in <- ingest.p("Currency_Neutral_Factors.csv")

## ingest the file
DT.Currency_Neutral_Factors <- fread(f.in)

if ("fx_neutralizing_factor" %in% names(DT.Currency_Neutral_Factors))
  setnames(DT.Currency_Neutral_Factors, "fx_neutralizing_factor", "fx_neutralizing_factor")

if (.Pfm != "Darwin") {
  ingestIntoSQL(DT.Currency_Neutral_Factors, datetime_type="TIMESTAMP_NTZ", tbl=tbl, schema=schema, snowflake_inuse=TRUE, drop=TRUE)

  sourceSupportFns(proj="Looker")
  f.lookml <- create_lookml_from_tbl(tbl=tbl, schema=schema)

  quickEmail(to=getRS(), file=f.lookml, info="Currency_Neutral_Factors")
}




#################

##   I believe this next part was scrap work that is no longer needed. 

#################

loadIfNotExists("DT.revshare", proj="Spotify_Accounting_ETL")
# DT.revshare already has a quarter_and_year column
stopifnot("quarter_and_year" %in% names(DT.revshare))

## add in fx_neutralizing_factor;  all NAs should be 1
addColsFrom_(DT.revshare, DT.Currency_Neutral_Factors, joinCols.rec=c("quarter_and_year", "currency"), joinCols.giv=c("quarter", "currency_code"))
DT.revshare[is.na(fx_neutralizing_factor), fx_neutralizing_factor := 1]

colsToAdjust <- c(orchard_gross_revenue="payable_usd", spotify_gross_revenue="gross_revenue", spotify_net_revenue="net_revenue")
newCols <- colNamesFromVector(colsToAdjust) %>% paste0("_neutral")
DT.revshare[, (newCols) := {lapply(colsToAdjust, function(col) get(col) * fx_neutralizing_factor)}]

ingestIntoSQL(DT.revshare, schema="SPOTIFYMGMT", intColsToCoerce=NULL, datetime_type="TIMESTAMP_NTZ", drop=TRUE, snowflake=TRUE)

if (FALSE) {
  .g()
  sourceSupportFns(proj="Looker")
  cls(); create_lookml_from_tbl(schema="SPOTIFYMGMT", tbl="revshare")
  combine_looker_exports_to_excel(fold)
}
