url_nc_cf_search_doc <- function() {
  "https://cf.ncsbe.gov/CFDocLkup/DocumentResult/"
}

url_nc_cf_export_search_doc <- function() {
  "https://cf.ncsbe.gov/CFDocLkup/ExportSearchResults/"
}

url_nc_cf_export_detail_results  <- function() {
  "https://cf.ncsbe.gov/CFOrgLkup/ExportDetailResults/"
}

url_nc_cf_report_get_receipts <- function() {
  "https://cf.ncsbe.gov/CFOrgLkup/GetReceipts"
}

url_nc_cf_report_get_expenditures <- function() {
  "https://cf.ncsbe.gov/CFOrgLkup/GetExpenditures"
}

match_report_type <- function(report, collapse = TRUE) {
  report <- toupper(report)

  reports <- c("RPMYSA", "RPYESA", "RPQTR1", "RPQTR2", "RPQTR3", "RPQTR4")
  valid <- c(
    reports,
    sub("^RP", "", reports),
    gsub("^RP|SA$", "", reports),
    sub("RPQTR", "Q", reports)
  )
  names(valid) <- rep(reports, 4)

  x <- names(valid)[match(report, valid)]
  if (any(is.na(x))) {
    report <- report[is.na(x)]
    cli::cli_abort(c("Invalid report {.val {report}}", i = "Valid: {.val {reports}}"))
  }

  if (!collapse) return(x)

  paste(paste0("'", x, "'"), collapse = ", ")
}

req_report_by_year <- function(
  year,
  report = c("RPMYSA", "RPYESA", "RPQTR1", "RPQTR2", "RPQTR3", "RPQTR4")
) {
  reports <- match_report_type(report)

  req <- request(url_nc_cf_search_doc())
  req <- req_url_query(req, year = year, reports = reports)

  req
}

req_report_by_year_export <- function(
  year,
  report = c("RPMYSA", "RPYESA", "RPQTR1", "RPQTR2", "RPQTR3", "RPQTR4")
) {
  reports <- match_report_type(report)

  req <- request(url_nc_cf_export_search_doc())
  req <- req_url_query(req, year = year, reports = reports)

  req
}

match_report_sections <- function(section) {
  options <- c(ALL = "all", CVR = "cover", EXP = "expenditures", REC = "receipts", EXP = "expenses")
  section <- arg_match(section, options)
  names(options)[which(section == options)]
}

req_report_detail <- function(report_id, section = "receipts") {
  section <- match_report_sections(section)

  req <- request(url_nc_cf_export_detail_results())
  # Title is needed or the page throws an error
  # https://cf.ncsbe.gov/CFOrgLkup/ExportDetailResults/?ReportID=197247&Type=REC&Title=JOHN
  req <- req_url_query(req, ReportID = report_id, Type = section, Title = "download")

  req
}

req_report_receipts <- function(report_id, page = 0, page_size = 300) {
  req <- request(url_nc_cf_report_get_receipts())
  req <- req_url_query(
    req,
    ReportID = report_id,
    page = page,
    pageSize = page_size
  )

  req
}

req_report_expenditures <- function(report_id, page = 0, page_size = 300) {
  req <- request(url_nc_cf_report_get_expenditures())
  req <- req_url_query(
    req,
    ReportID = report_id,
    page = page,
    pageSize = page_size,
    ShowIEColumns = TRUE # this doesn't appear to do anything, but it's required
  )

  req
}
