#' Write an authordown input template
#'
#' @description Copies a standard author metadata template from the package
#' into the requested path. Use this to start a new project with the expected
#' columns and example values. If `path` is omitted, a temporary file is used.
#'
#' @param path Output file path. If omitted, a temporary file is created.
#'   Extension determines format if \code{format} is not supplied.
#' @param format Optional format override: \code{"csv"} or \code{"xlsx"}.
#' @return Invisibly returns the output path.
#' @export
#' @examples
#' csv_path <- authordown_template()
#' xlsx_path <- authordown_template(format = "xlsx")
authordown_template <- function(path = NULL, format = NULL) {
  if (is.null(format)) {
    if (is.null(path)) {
      format <- "csv"
    } else {
      format <- tolower(tools::file_ext(path))
      if (format == "") {
        format <- "csv"
      }
    }
  }
  format <- match.arg(format, c("csv", "xlsx"))
  if (is.null(path)) {
    path <- tempfile("authordown_template_", fileext = paste0(".", format))
  }

  source_path <- system.file(
    "extdata",
    paste0("authordown_template.", format),
    package = "authordown"
  )
  if (source_path == "") {
    rlang::abort("Template file is missing from inst/extdata.")
  }

  ok <- file.copy(source_path, path, overwrite = TRUE)
  if (!ok) {
    rlang::abort(paste0("Failed to write template to: ", path))
  }

  invisible(path)
}
