library(completejourney)
library(dplyr)
library(ggplot2)
library(stringr)
library(lubridate)

productse2 = products %>% inner_join(transactions_sample, by = "product_id") %>% 
  inner_join(demographics, by = "household_id") 

productse2$manufacturer_id = factor(productse2$manufacturer_id, levels = 
                                      c("2","5","67","69","103","192",
                                        "509","539","697","1058","1225",
                                        "1847","2558","2571","2887","3500",
                                        "3637","4339","4424","5220","5853",
                                        "5873","6046","6217","6290"))

productse2 %>% arrange(desc(sales_value)) %>%
  slice(1:100) %>% ggplot(aes(manufacturer_id, sales_value)) + geom_point() + 
  scale_x_discrete("Manufacturer ID", guide = guide_axis(n.dodge=2)) + 
  scale_y_continuous("Sales Value") + ggtitle(
    "Which Manufacturer has the highest Sales Value", subtitle = 
      "Given top 100 sales values, Manufacturer 69 has the most sales with high value"
  )

transactions = completejourney::transactions_sample

amcount = products %>% filter(str_detect(department, "COSMETICS")) %>% 
  inner_join(transactions, by= "product_id") %>% 
  inner_join(demographics, by= "household_id") %>% 
  mutate(month = month(transaction_timestamp)) %>% 
  count(age,month) %>% 
  mutate(month = recode(month, "1" = "January", "2" = "February", "3" = "March", "4" = "April", 
                                      "5" = "May", "6" = "June", "7" = "July", "8" = "August", "9" = "September",
                                      "10" = "October", "11" = "November", "12" = "December")) 

amcount$month = factor(amcount$month, levels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))

amcount %>% mutate(Age = age) %>%
  group_by(Age, month) %>%
  summarize(total_count = sum(n)) %>%
  ungroup() %>%
  group_by(month) %>% arrange(desc(total_count)) %>% 
  select(Age, month, total_count) %>% 
  ggplot(aes(Age, total_count)) +
  geom_col() + facet_wrap(~month) + scale_x_discrete(guide = guide_axis(n.dodge=2)) + 
  scale_y_continuous("Sales") + ggtitle("Who's buying the most Cosmetic Products, and when?",
                                        subtitle = "Ages 35-54 in the months of December & May buy the most cosmetic products")
## `summarise()` has grouped output by 'Age'. You can override using the `.groups`
## argument.

productse3 = products %>% inner_join(transactions_sample, by = "product_id") %>% 
  inner_join(demographics, by = "household_id") 

productse3 %>% filter(str_detect(product_category,(regex("Eggs|Cheese$", ignore_case= TRUE)))) %>% 
  ggplot(aes(product_category, retail_disc)) + geom_col() + scale_x_discrete("Product Category") + 
  scale_y_continuous("Total Retail Discount") + 
  ggtitle("Total Retail Discount of Eggs and Cheese", 
          subtitle = "Cheese has a higher overall retail discount than Eggs")