# 1. LOAD LIBRARIES
library(tidyverse)
library(lubridate)
library(scales)
library(patchwork)

# 2. DATA SIMULATION (To replicate the PDF visual trends)
set.seed(123)
dates <- seq(as.Date("2011-01-01"), as.Date("2015-12-31"), by="month")

# Create data for Road and Mountain categories as seen in the PDF
categories <- data.frame(
  Category1 = c(rep("Road", 4), rep("Mountain", 5)),
  Category2 = c("Elite Road", "Endurance Road", "Triathalon", "Cyclocross",
                "Cross Country Race", "Trail", "Over Mountain", "Sport", "Fat Bike")
)

bike_data <- expand.grid(Date = dates, Category2 = categories$Category2) %>%
  left_join(categories, by = "Category2") %>%
  mutate(Revenue = runif(n(), 50000, 500000))

# 3. TOTAL SALES CHARTS (Page 1 & 2)
# Quarterly Trends
p1 <- bike_data %>%
  mutate(Quarter = floor_date(Date, "quarter")) %>%
  group_by(Quarter) %>%
  summarise(Total_Revenue = sum(Revenue)) %>%
  ggplot(aes(x = Quarter, y = Total_Revenue)) +
  geom_line(color = "steelblue", size = 1) +
  scale_y_continuous(labels = label_dollar()) +
  labs(title = "Total Sales Charts", subtitle = "Quarterly Sales Trends", y = "Revenue (USD)", x = "") +
  theme_minimal()

# Monthly Trends
p2 <- bike_data %>%
  group_by(Date) %>%
  summarise(Total_Revenue = sum(Revenue)) %>%
  ggplot(aes(x = Date, y = Total_Revenue)) +
  geom_line(color = "firebrick", size = 0.8) +
  scale_y_continuous(labels = label_dollar()) +
  labs(subtitle = "Monthly Sales Trends", y = "Revenue (USD)", x = "") +
  theme_minimal()

# 4. CATEGORY SALES CHARTS - ROAD (Page 3-5)
# Replicating the faceted layout for Road
road_plot <- bike_data %>%
  filter(Category1 == "Road") %>%
  ggplot(aes(x = Date, y = Revenue)) +
  geom_line(color = "blue") +
  facet_wrap(~Category2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K")) +
  labs(title = "Category Sales Charts: Road", subtitle = "Monthly Sales By Category 2", y = "Revenue ($K)", x = "") +
  theme_minimal()

# 5. CATEGORY SALES CHARTS - MOUNTAIN (Page 6)
# Replicating the faceted layout for Mountain
mountain_plot <- bike_data %>%
  filter(Category1 == "Mountain") %>%
  mutate(Quarter = floor_date(Date, "quarter")) %>%
  group_by(Quarter, Category2) %>%
  summarise(Revenue = sum(Revenue)) %>%
  ggplot(aes(x = Quarter, y = Revenue)) +
  geom_line(color = "darkgreen") +
  facet_wrap(~Category2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K")) +
  labs(title = "Category Sales Charts: Mountain", subtitle = "Quarterly Sales By Category 2", y = "Revenue ($K)", x = "") +
  theme_minimal()

# 6. PRINTING OUTPUTS
p1

p2

road_plot

mountain_plot