# load_clean_zillow.R
# Loads and tidies the Zillow metro datasets used in week8, and defines
# clean_it() and get_states() for reuse.
#
# Requires a "zdata" folder in the SAME FOLDER you source this from --
# run download_zillow_data.R once first (see week8/zillow-load.qmd) to
# create it. No project setup or `here` package needed: just keep this
# script, your .qmd, and zdata/ together.

library(tidyverse)

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- #
#  data: all from https://www.zillow.com/research/data/
#  run download_zillow_data.R to (re)create zdata/
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- #

# absolute directory
zdir <- '~/g/teaching/dat309/week8/'

# yts - years to save for a 20% down payment, metro-level
yts <- read_csv(paste0(
  zdir,
  'zdata/Metro_years_to_save_downpayment_0.20_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv'
)) |>
  janitor::clean_names()
message("dataset yts loaded")

# nrin - new renter income needed, metro-level
nrin <- read_csv(paste0(
  zdir,
  "zdata/Metro_new_renter_income_needed_uc_sfrcondomfr_sm_sa_month.csv"
)) |>
  janitor::clean_names()
message("dataset nrin loaded")

# nra - new renter affordability (% of income spent on rent), metro-level
nra <- read_csv(paste0(
  zdir,
  "zdata/Metro_new_renter_affordability_uc_sfrcondomfr_sm_sa_month.csv"
)) |>
  janitor::clean_names()
message("dataset nra loaded")

# Pivots one Zillow wide-format dataset (one column per month, named
# like x2020_01_31) into long format.
#   data:   a data frame read in above (e.g. nrin, yts, nra)
#   values: name to give the value column, e.g. "years_to_save"
clean_it <- function(data, values) {
  data <- pivot_longer(
    data,
    cols = starts_with("x"),
    names_to = "date",
    values_to = values
  )
  data |> mutate(date = as_date(str_replace(date, "x", "")))
}

# Pulls a random sample of `number` regions from each state in `name`.
#   data:   a data frame already cleaned by clean_it()
#   name:   vector of 2-letter state codes, e.g. c("MI", "NC")
#   number: how many regions to sample per state
# usage: get_states(clean_it(yts, "years_to_save"), c("MI", "NC"), 3)
get_states <- function(data, name, number) {
  d <- list()
  for (i in seq_along(name)) {
    my_state <- filter(data, state_name %in% name[i])
    some_names <- distinct(my_state, region_name) |> slice_sample(n = number)
    d[[i]] <- filter(my_state, region_name %in% pull(some_names))
  }
  bind_rows(d)
}
