##############################################################################################
#' @title Unit test of def.pub.tabl.crea.R, create publication table

#' @author
#' Mija Choi \email{choim@battelleecology.org}

#' @description
#' Definition function.


#' @param data A list of Arrow datasets, each list element containing an arrow dataset as retrieved
#' by arrow::open_dataset. These datasets contain time timeseries data from which the data for the
#' publication table will be gathered from. Note that the datasets all must have the same number of rows.
#' @param pubWb A data from of the publication workbook, as generated by NEONprocIS.pub::def.read.pub.wb
#' @param TablPub Optional. Character value indicating the publication table to produce. If not input or
#' the value is NULL, the entire pub workbook will be created. Note that duplicated field names in the
#' pubWb (after filtering for the table of interest) will be removed.
#' @param log A logger object as produced by NEONprocIS.base::def.log.init to produce structured log
#' output. Defaults to NULL, in which the logger will be created and used within the function.

#' @return A named list:
#' dataTabl: A data frame of the publication table. Note that the column order is determined by the
#'           order of the rows in the publication workbook, not from the Rank field. This is intentional,
#'           so as to avoid conflicts if there are duplicated field names or Rank values.
#' nameVarMtch: A character vector with the names of the variables in the pub table that were found in
#'              the input datasets.

#' @references
#' License: (example) GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007

#' @keywords Currently none

#' @examples
#' Not Run
#' FileData <- c('/path/to/file1.parquet','/path/to/file2.parquet') # Files must have same # of rows
#' data <- base::lapply(FileData,arrow::open_dataset)
#' FilePubWb <- 'path/to/publication/workbook.txt'
#' pubWb <- NEONprocIS.pub::def.read.pub.wb(NameFile=FilePubWb)
#'
#' # changelog and author contributions / copyrights
#   Mija Choi (2023-04-19)
#      Original Creation
##############################################################################################
test_that("   Testing def.pub.tabl.crea.R, Create publication table",
          {
            wk_dir <- getwd()
            
            #1. Happy path, TablPub is not passed in
            FileData <- 'pfs/pubWb/par-quantum-line_CPER001000_2023-02-03_PARQL_1min_001.parquet'
            data <- base::lapply(FileData, arrow::open_dataset)
            FilePubWb = 'pfs/pubWb/PublicationWorkbook_elevSurfacewater.txt'
            pubWb <- NEONprocIS.pub::def.read.pub.wb(NameFile = FilePubWb)
            returnedList <- NEONprocIS.pub::def.pub.tabl.crea(data = data, pubWb = pubWb)
            expect_true(any(returnedList$nameVarMtch %in% pubWb$fieldName))
            
            #2. TablPub = 'EOS_30_min' exists in the workbook
            FileData <- 'pfs/pubWb/par-quantum-line_CPER001000_2023-02-03_PARQL_1min_001.parquet'
            returnedList <- NEONprocIS.pub::def.pub.tabl.crea(data = data,
                                                pubWb = pubWb,
                                                TablPub = 'EOS_30_min')
            expect_true(any(returnedList$nameVarMtch %in% pubWb$fieldName))
            
            #3. TablPub = 'EOS_1_min' does not exist in the workbook
            returnedList <- NEONprocIS.pub::def.pub.tabl.crea(data = data,
                                                pubWb = pubWb,
                                                TablPub = 'EOS_1_min')
            expect_identical(returnedList$nameVarMtch, character(0))
          }
        )

