#' # 🔍 Let’s Solve the Mystery!
#' 
#' Run these lines to attach the tidyverse to your
#' current R session along with the tibbles from the
#' police database.

library(tidyverse)
people <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/person.csv")
drivers_license <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/drivers_license.csv")
income <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/income.csv")
crime_scene_report <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/crime_scene_report.csv")
facebook_event_checkin <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/facebook_event_checkin.csv")
get_fit_now_check_in <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/get_fit_now_checkin.csv")
get_fit_now_member <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/get_fit_now_member.csv")
interview <- read_csv("https://raw.githubusercontent.com/cobriant/dplyrmurdermystery/master/data/interview.csv")
print_all <- function(df, row, col) cat(str_wrap(df[row, col], width = 80))

#' 
#' ## QUESTION 1:  Start with the Crime Scene Report.
#' 
#' The detective tells you the crime was a **murder** 
#' that happened on **January 15th, 2018**, in 
#' **"dplyr City"**. I’ve started a query for you, but 
#' it’s not quite right. Can you fix it to find the 
#' correct report?

crime_scene_report %>%
  filter(date == "2018-01-15", city == "NYC")

#' Note: are you annoyed by not being able to easiy read 
#' these long character strings because it truncates them? 
#' I made a function for you called `print_all()` to help. 
#' It takes a tibble as the first argument, the (integer) 
#' row you want to print as the second argument, and the 
#' (integer) column you want to print as the third argument. 
#' For example, this will print the long string in row 10, 
#' column 3 of `crime_scene_report`:

crime_scene_report %>%
  print_all(10, 3)

#' ## QUESTION 2: Find the First Witness
#' 
#' Once you’ve read the crime scene report, you’ll have clues to 
#' identify the witnesses. Write a query to find the first witness.

# people %>%
#   filter() %>%
#   arrange()

#' ## QUESTION 3: Find the Second Witness
#' 
#' Now, write a query to find the second witness.

# people %>%
#   filter()

#' ## QUESTION 4: Review the Witness Interviews
#' 
#' Next, let’s see what the witnesses had to say. Write two 
#' queries to pull the interview transcripts for both witnesses.

# interview %>%
#   filter()

# interview %>%
#   filter()

#' ## QUESTION 5: Solve the Mystery!
#' 
#' You’ve got all the clues you need. Now it’s time to crack the 
#' case! Write your final queries to solve the mystery.
#' 
#' 🎯 **Tip: Use left_join() to Connect the Dots**
#' To create a clean and elegant solution here, try using 
#' `left_join()`. This function lets you combine tables based on 
#' shared keys. For example, to find driver’s license and income 
#' information for everyone named Amy, you could do this:

people %>%
  filter(str_detect(name, "Amy")) %>%
  left_join(drivers_license, join_by(license_id == id)) %>%
  left_join(income, join_by(ssn))

#' Check your final answer by filling out the autograder for this 
#' assignment.
