#' Generate a sample CSV template for authordown
#'
#' @description Creates a sample CSV (or Excel) with columns for authors,
#' affiliations, acknowledgments, conflicts, etc. If `file` is omitted, a
#' temporary file is created.
#' @param file A file path where the template should be written. If omitted,
#'   a temporary file is created.
#' @param excel Logical. If TRUE, writes an Excel file (.xlsx) instead of CSV.
#' @return Invisibly returns the data frame used for the template.
#' @export
#' @examples
#' generate_template()
#' generate_template(excel = TRUE)
generate_template <- function(file = NULL, excel = FALSE) {
  format <- if (isTRUE(excel)) "xlsx" else "csv"
  if (is.null(file)) {
    file <- tempfile("authordown_template_", fileext = paste0(".", format))
  } else if (!grepl(paste0("\\.", format, "$"), file, ignore.case = TRUE)) {
    file <- paste0(file, ".", format)
  }

  authordown_template(path = file, format = format)
  message("Template written to: ", file)

  invisible(authordown_read_local(file, validate = FALSE))
}
