# WARNING - Generated by {fusen} from dev/flat_utils.Rmd: do not edit by hand

#' Perform a classical dplyr::left_join() and add check information related to join
#'
#' @param ltable Data.frame. Left data frame in the join
#' @param rtable Data.frame. Right data frame in the join
#' @param ... Any other arguments passed to \code{dplyr::left_join()}.
#' @return Data.frame. Output of dplyr::left_join() with messages on number of observations
#' in left, right and joined data frames and list of common variables between
#' ltable and rtable
#' @importFrom magrittr  %>%
#' @export
#'
#' @examples
#' left_table <- data.frame("person_id" = c(1, 1, 2, 3,
#'                                  2, 4, 5, 5 ,1),
#'                  "person_age" = c(25, 25, 21, 32,
#'                                   21, 48, 50, 50, 52),
#'                  "survey_month" = c("jan", "feb", "mar", "apr",
#'                                     "apr", "may", "jun", "jul", "jan"),
#'                  "survey_answer" = c("no", "yes", "no", "yes",
#'                                      "yes", "yes", "no", "yes", NA))
#'
#' right_table <- data.frame("person_id" = c(2, 5, 4, 3, 1), 
#'                           "person_name" = c("John", "Marie", "Pierre", "Marc", "Jimmy"))
#'
#' list("left_table" = left_table, 
#'      "right_table" = right_table)
#'
#' ljoin_checks(left_table, right_table, by = "person_id")
ljoin_checks <- function (ltable, rtable, ...) {

  requireNamespace("magrittr")

  result <- dplyr::left_join(x = ltable, y = rtable, ...)

  lrows <- nrow(ltable)
  rrows <- nrow(rtable)
  jrows <- nrow(result)

  lvars <- names(ltable)
  rvars <- names(rtable)
  ivars <- intersect(lvars, rvars)

  message("Checks : \n",
          paste0("ltable rows : ", lrows),
          "\n",
          paste0("rtable rows :", rrows),
          "\n",
          paste0("jtable rows : ", jrows),
          "\n",
          paste0(ivars, collapse = ", ") %>%
            paste0(" are common var names accross the two tables"))

  return(result)
}
