library(tidyverse)
library(lubridate)
library(scales)
sales_data_raw <- read_csv("bike_orderlines.csv")
sales_data <- sales_data_raw %>%
  mutate(
    order_date = as.Date(order_date),
    year = year(order_date)
  ) %>%
  select(order_date, category_1, category_2, total_price)
# Quarterly aggregation
total_quarterly <- sales_data %>%
  mutate(date_rounded = floor_date(order_date, unit = "quarter")) %>%
  group_by(date_rounded) %>%
  summarise(total_revenue = sum(total_price))

# Monthly aggregation
total_monthly <- sales_data %>%
  mutate(date_rounded = floor_date(order_date, unit = "month")) %>%
  group_by(date_rounded) %>%
  summarise(total_revenue = sum(total_price))

# Weekly aggregation
total_weekly <- sales_data %>%
  mutate(date_rounded = floor_date(order_date, unit = "week")) %>%
  group_by(date_rounded) %>%
  summarise(total_revenue = sum(total_price))
# Category quarterly
category_quarterly <- sales_data %>%
  mutate(date_rounded = floor_date(order_date, unit = "quarter")) %>%
  group_by(category_1, category_2, date_rounded) %>%
  summarise(total_revenue = sum(total_price)) %>%
  ungroup()

# Category monthly
category_monthly <- sales_data %>%
  mutate(date_rounded = floor_date(order_date, unit = "month")) %>%
  group_by(category_1, category_2, date_rounded) %>%
  summarise(total_revenue = sum(total_price)) %>%
  ungroup()

# Category weekly
category_weekly <- sales_data %>%
  mutate(date_rounded = floor_date(order_date, unit = "week")) %>%
  group_by(category_1, category_2, date_rounded) %>%
  summarise(total_revenue = sum(total_price)) %>%
  ungroup()

SECTION 1: TOTAL BIKE SALES

# Chart 1: Total Sales - Quarterly Trends
p1 <- ggplot(total_quarterly, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 2, color = "#2c3e50") +
  geom_line(color = "#2c3e50") +
  geom_smooth(method = "loess", span = 0.3, color = "blue", se = FALSE) +
  scale_y_continuous(labels = dollar_format(scale = 1e-6, suffix = "M")) +
  theme_bw() +
  labs(title = "Total Sales: Quarterly Trends", x = "", y = "Revenue (USD)")

print(p1)

# Chart 2: Total Sales - Monthly Trends
p2 <- ggplot(total_monthly, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1.5, color = "#2c3e50") +
  geom_line(color = "#2c3e50", alpha = 0.3) +
  geom_smooth(method = "loess", span = 0.1, color = "blue", se = TRUE) +
  scale_y_continuous(labels = dollar_format(scale = 1e-6, suffix = "M")) +
  theme_bw() +
  labs(title = "Total Sales: Monthly Trends", x = "", y = "Revenue (USD)")

print(p2)

# Chart 3: Total Sales - Weekly Trends
p3 <- ggplot(total_weekly, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1, color = "#2c3e50", alpha = 0.5) +
  geom_smooth(method = "loess", span = 0.1, color = "blue", se = TRUE) +
  scale_y_continuous(labels = dollar_format(scale = 1e-6, suffix = "M")) +
  theme_bw() +
  labs(title = "Total Sales: Weekly Trends", x = "", y = "Revenue (USD)")

print(p3)

SECTION 2: ROAD BIKE SALES

# Filter for Road Bikes
road_qt <- category_quarterly %>% filter(category_1 == "Road")
road_mo <- category_monthly %>% filter(category_1 == "Road")
road_wk <- category_weekly %>% filter(category_1 == "Road")
# Chart 4: Road - Quarterly
p4 <- ggplot(road_qt, aes(x = date_rounded, y = total_revenue)) +
  geom_line(color = "black") +
  geom_point(size = 1.5) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Road Sales: Quarterly", x = "", y = "Revenue")

print(p4)

# Chart 5: Road - Monthly
p5 <- ggplot(road_mo, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1.5, alpha = 0.5) +
  geom_smooth(method = "loess", span = 0.3, color = "red", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Road Sales: Monthly", x = "", y = "Revenue")

print(p5)

# Chart 6: Road - Weekly
p6 <- ggplot(road_wk, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1, alpha = 0.3) +
  geom_smooth(method = "loess", span = 0.1, color = "red", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Road Sales: Weekly", x = "", y = "Revenue")

print(p6)

SECTION 3: MOUNTAIN BIKE SALES

# Filter for Mountain Bikes
mtn_qt <- category_quarterly %>% filter(category_1 == "Mountain")
mtn_mo <- category_monthly %>% filter(category_1 == "Mountain")
mtn_wk <- category_weekly %>% filter(category_1 == "Mountain")
# Chart 7: Mountain - Quarterly
p7 <- ggplot(mtn_qt, aes(x = date_rounded, y = total_revenue)) +
  geom_line(color = "black") +
  geom_point(size = 1.5) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Mountain Sales: Quarterly", x = "", y = "Revenue")

print(p7)

# Chart 8: Mountain - Monthly
p8 <- ggplot(mtn_mo, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1.5, alpha = 0.5, color = "#2ecc71") +
  geom_smooth(method = "loess", span = 0.3, color = "#27ae60", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Mountain Sales: Monthly", x = "", y = "Revenue")

print(p8)

# Chart 9: Mountain - Weekly
p9 <- ggplot(mtn_wk, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1, alpha = 0.3, color = "#2ecc71") +
  geom_smooth(method = "loess", span = 0.1, color = "#27ae60", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Mountain Sales: Weekly", x = "", y = "Revenue")

print(p9)