# Created by use_targets().
# Follow the comments below to fill in this target script.
# Then follow the manual to check and run the pipeline:
#   https://books.ropensci.org/targets/walkthrough.html#inspect-the-pipeline

# Load packages required to define the pipeline:
library(targets)

# Set target options:
tar_option_set(
  packages = strsplit(desc::desc_get_field("Depends"), ", ")[[1]],
  # For distributed computing in tar_make(), supply a {crew} controller
  # as discussed at https://books.ropensci.org/targets/crew.html.
  controller = crew::crew_controller_local(workers = 24),
  # debug = "path_receipts_parquet_8d195f7e",
  # cue = tar_cue(mode = "never")
  error = "null"
)

# Run the R scripts in the R/ folder with your custom functions:
tar_source()

# Replace the target list below with your own:
list(
  tar_target(path_report_list_csv, "../data-raw/report_list.csv", format = "file"),
  tar_target(path_report_list_raw, prepare_report_list(path_report_list_csv)),
  tar_target(report_list_raw, arrow::read_parquet(path_report_list_raw)),

  tar_target(
    dirs_all,
    fs::dir_ls("../data-raw/reports", glob = "**/all", recurse = TRUE, type = "directory"),
    format = "file"
  ),
  tar_target(
    dirs_receipts,
    fs::dir_ls("../data-raw/reports", glob = "**/receipts", recurse = TRUE, type = "directory"),
    format = "file"
  ),
  tar_target(
    dirs_expenditures,
    fs::dir_ls("../data-raw/reports", glob = "**/expenditures", recurse = TRUE, type = "directory"),
    format = "file"
  ),

  tar_target(
    paths_all_parquet,
    write_prepared_report_export(dirs_all, report_list_raw),
    pattern = map(dirs_all),
    format = "file"
  ),

  tar_target(
    path_receipts_parquet,
    write_prepared_receipts_parquet(dirs_receipts, report_list_raw),
    pattern = map(dirs_receipts),
    format = "file"
  ),

  tar_target(
    path_expenditures_parquet,
    write_prepared_expenditures_parquet(dirs_expenditures, report_list_raw),
    pattern = map(dirs_expenditures),
    format = "file"
  ),

  tar_target(path_data_prep_cover, { paths_all_parquet; "../data-prep/cover" }, format = "file"),
  tar_target(path_data_prep_officers, { paths_all_parquet; "../data-prep/officers" }, format = "file"),


  tar_target(
    cover_raw,
    arrow::open_dataset(path_data_prep_cover, partitioning = "sboe_id") |> dplyr::collect()
  ),

  tar_target(
    report_dates,
    process_report_dates(report_list_raw, cover_raw)
  ),

  tar_target(
    report_amended_score,
    calc_report_amended_score(report_dates)
  ),

  tar_target(
    report_list,
    process_report_list(report_list_raw, report_amended_score)
  ),

  tar_target(committees, prepare_committees(cover_raw, report_list)),

  tar_target(candidates, prepare_candidates(path_data_prep_officers, report_list))
)
