# download_zillow_data.R
#
# Run this once per semester (and again whenever you want fresher data),
# from the SAME FOLDER as your .qmd files -- e.g. open it in Positron and
# click Source, or run source("download_zillow_data.R") from the console.
# It creates a "zdata" folder right here (no project setup, no `here`
# package) and downloads the Zillow Research CSVs load_clean_zillow.R
# expects to find in it.
#
# Zillow revises history in place, so re-running this later can change
# values you've already used in class, not just add new months -- that's
# why the script stamps a "last downloaded" date into zdata/.
#
# Before running: open https://www.zillow.com/research/data/ and
# confirm/paste the three URLs marked TODO below (nrin, nra, yts) -- as
# of 2026-10-12 these three weren't verifiable as plain links from the
# page; the two below (zhvi, zori) were confirmed directly.

dir.create("zdata", showWarnings = FALSE)

urls <- c(
  # confirmed 2026-10-12 -- not currently used by load_clean_zillow.R,
  # but already part of your zdata set for other exercises
  "https://files.zillowstatic.com/research/public_csvs/zhvi/Metro_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv",
  "https://files.zillowstatic.com/research/public_csvs/zori/Metro_zori_uc_sfrcondomfr_sm_month.csv",
  # TODO -- paste the current URLs from https://www.zillow.com/research/data/
  "TODO: paste current New Renter Income Needed URL here",
  "TODO: paste current New Renter Affordability URL here",
  "TODO: paste current Years to Save (20% down) URL here"
)

for (url in urls) {
  if (startsWith(url, "TODO")) {
    warning("Skipping a TODO URL -- update download_zillow_data.R first: ", url)
    next
  }
  dest <- file.path("zdata", basename(url))
  download.file(url, destfile = dest, mode = "wb", quiet = TRUE)
  message("Downloaded ", basename(dest))
}

writeLines(
  paste("zdata downloaded:", Sys.Date()),
  file.path("zdata", "README_last_updated.txt")
)
