---
title: "Monthly Business Report"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(DT)
library(plotly)
library(dplyr)

# Generate sample data
set.seed(123)
sales_data <- data.frame(
  Month = month.name,
  Sales = round(runif(12, 50000, 150000), 0),
  Profit = round(runif(12, 10000, 50000), 0),
  Customers = round(runif(12, 500, 2000), 0)
)
```

Sidebar {.sidebar}
=====================================

```{r}
selectInput("metric", "Select Metric:",
  choices = c("Sales", "Profit", "Customers"),
  selected = "Sales"
)

selectInput("chart_type", "Chart Type:",
  choices = c("Bar Chart", "Line Chart"),
  selected = "Bar Chart"
)

hr()

h4("Report Summary")
p("This interactive report shows monthly business metrics including sales, profit, and customer data.")
```

Column
=====================================

### Monthly Trends

```{r}
renderPlotly({
  metric_col <- input$metric

  if (input$chart_type == "Bar Chart") {
    p <- plot_ly(sales_data,
      x = ~Month, y = ~ get(metric_col), type = "bar",
      name = metric_col
    ) %>%
      layout(
        title = paste("Monthly", metric_col),
        xaxis = list(title = "Month"),
        yaxis = list(title = metric_col)
      )
  } else {
    p <- plot_ly(sales_data,
      x = ~Month, y = ~ get(metric_col), type = "scatter", mode = "lines+markers",
      name = metric_col
    ) %>%
      layout(
        title = paste("Monthly", metric_col, "Trend"),
        xaxis = list(title = "Month"),
        yaxis = list(title = metric_col)
      )
  }

  p
})
```

### Key Performance Indicators

```{r}
renderUI({
  metric <- input$metric
  total_value <- sum(sales_data[[metric]])
  avg_value <- round(mean(sales_data[[metric]]), 0)
  max_month <- sales_data$Month[which.max(sales_data[[metric]])]

  fluidRow(
    column(
      4,
      div(
        class = "well",
        h3(format(total_value, big.mark = ","), style = "color: #337ab7;"),
        p(paste("Total", metric))
      )
    ),
    column(
      4,
      div(
        class = "well",
        h3(format(avg_value, big.mark = ","), style = "color: #5cb85c;"),
        p(paste("Average", metric))
      )
    ),
    column(
      4,
      div(
        class = "well",
        h3(max_month, style = "color: #f0ad4e;"),
        p(paste("Best Month for", metric))
      )
    )
  )
})
```

Column
=====================================

### Data Table

```{r}
renderDT({
  datatable(sales_data,
    options = list(
      pageLength = 12,
      dom = "tp",
      columnDefs = list(list(className = "dt-center", targets = 1:3))
    ),
    rownames = FALSE
  ) %>%
    formatCurrency(c("Sales", "Profit"), currency = "$", interval = 3, mark = ",") %>%
    formatRound("Customers", digits = 0)
})
```

### Monthly Comparison

```{r}
renderPlotly({
  p <- plot_ly(sales_data,
    x = ~Month, y = ~Sales, type = "bar", name = "Sales",
    yaxis = "y", offsetgroup = 1
  ) %>%
    add_trace(y = ~Profit, name = "Profit", yaxis = "y2", offsetgroup = 2) %>%
    layout(
      title = "Sales vs Profit Comparison",
      xaxis = list(title = "Month"),
      yaxis = list(side = "left", title = "Sales ($)", showgrid = FALSE, zeroline = FALSE),
      yaxis2 = list(side = "right", overlaying = "y", title = "Profit ($)", showgrid = FALSE, zeroline = FALSE),
      hovermode = "x",
      legend = list(x = 0.029, y = 1.038, font = list(size = 10))
    )

  p
})
```
