library(shiny)
library(tidyverse)

################################################################################
# Load and process flight data
################################################################################

cat("Loading flight data...\n")

# Load the raw data from BTS
flights_raw <- readRDS("../../data/flights_2024/flights_2024_top_15_raw.rds")

# Process the data: valid flights have both arr_time and arr_delay (not NA)
flights_processed <- flights_raw %>%
  mutate(
    cancelled = is.na(arr_time),
    valid = !is.na(arr_time) & !is.na(arr_delay)
  )

# Filter to valid flights only (completed with delay data)
valid_flights <- flights_processed %>%
  filter(valid)

# Compute population parameters
population_mean <- mean(valid_flights$arr_delay)
population_sd <- sd(valid_flights$arr_delay)
population_size <- nrow(valid_flights)

cat(sprintf("Population mean (μ): %.2f minutes\n", population_mean))
cat(sprintf("Population SD (σ): %.2f minutes\n", population_sd))
cat(sprintf("Population size: %d valid flights\n\n", population_size))

# Extract delay vector for sampling
delays <- valid_flights$arr_delay

################################################################################
# Precompute simulation data
################################################################################

n_universes <- 10000
n_values <- c(10, 100, 1000, 5000)

cat("Precomputing confidence interval data...\n")

# Create a data structure to hold all precomputed results
precomputed_data <- list()

set.seed(226)  # For reproducibility

for (n in n_values) {
  cat(sprintf("  Sample size n = %d\n", n))

  # Preallocate vectors for efficiency
  sample_means <- numeric(n_universes)
  lower_bounds <- numeric(n_universes)
  upper_bounds <- numeric(n_universes)

  for (i in 1:n_universes) {
    # Draw n flights with replacement from the population
    sample_delays <- sample(delays, size = n, replace = TRUE)

    # Compute sample mean
    y_bar <- mean(sample_delays)

    # Compute sample standard deviation
    sigma_hat <- sd(sample_delays)

    # Compute standard error
    se <- sigma_hat / sqrt(n)

    # Compute 95% confidence interval
    z <- qnorm(0.975)
    lower <- y_bar - z * se
    upper <- y_bar + z * se

    # Store results
    sample_means[i] <- y_bar
    lower_bounds[i] <- lower
    upper_bounds[i] <- upper
  }

  # Create data frame
  data <- data.frame(
    universe = 1:n_universes,
    lower = lower_bounds,
    upper = upper_bounds,
    y_bar = sample_means
  )

  # Order universes by lower bound of the interval
  data <- data[order(data$lower), ]
  data$universe <- 1:n_universes

  # Calculate coverage (fraction of intervals containing true mean)
  fraction_outside <- mean(population_mean < lower_bounds | population_mean > upper_bounds)
  fraction_inside <- 1 - fraction_outside

  # Store in list
  key <- paste0("n", n)
  precomputed_data[[key]] <- list(
    data = data,
    fraction_outside = fraction_outside,
    fraction_inside = fraction_inside
  )
}

cat("Precomputation complete!\n\n")

################################################################################
# Shiny App UI
################################################################################

ui <- fluidPage(
  titlePanel("Confidence Intervals for Mean Flight Arrival Delay"),

  # Add explanatory text at the top
  wellPanel(
    h4("Connection to Lecture 9: The Sample Mean"),
    p(HTML("This dashboard illustrates the <b>confidence interval concept from Lecture 9</b>,
           using the <b>flight delays example</b> discussed in class.")),

    p(HTML(sprintf("<b>Population:</b> All valid flights in 2024 from the top 15 US airports
           (N = %s flights).<br>
           <b>Population mean:</b> μ = %.2f minutes<br>
           <b>Population SD:</b> σ = %.2f minutes",
           format(population_size, big.mark = ","),
           population_mean,
           population_sd))),

    p(HTML("<b>Estimation:</b> We draw a random sample of size <i>n</i> flights (with replacement)
           and compute the <b>sample mean</b>:
           <br>&nbsp;&nbsp;&nbsp;&nbsp;<b>Ȳ = (1/n) Σ Y<sub>i</sub></b>")),

    p(HTML("<b>Central Limit Theorem:</b> For large <i>n</i>, the sample mean is approximately
           normally distributed: Ȳ ≈ N(μ, σ²/n).")),

    p(HTML("<b>Confidence Interval:</b> A 95% CI for μ is:
           <b>[Ȳ - 1.96·SE, Ȳ + 1.96·SE]</b>, where SE = σ̂/√n and σ̂ is the sample standard deviation.")),

    p(HTML("<b>This visualization:</b> Shows 10,000 'parallel universes', each with a different
           random sample of size <i>n</i>. Each universe constructs its own 95% confidence interval.
           The dashed line shows the true population mean μ.
           Approximately 95% of the intervals should contain μ."))
  ),

  sidebarLayout(
    sidebarPanel(
      selectInput("n_obs",
                  "Sample size (n)",
                  choices = n_values,
                  selected = 100),

      hr(),

      p(strong("Interpretation:")),
      p("• Each dot represents one endpoint of a confidence interval from one universe"),
      p("• Blue = lower bound, Red = upper bound"),
      p("• Dashed black line = true population mean μ"),
      p("• Intervals are sorted by their lower bound for visualization"),

      hr(),

      p(strong("Key insight:")),
      p("As sample size increases, confidence intervals become narrower
        (more precise estimates of μ).")
    ),

    mainPanel(
      plotOutput("ciPlot", height = "500px"),
      verbatimTextOutput("coverageOutput")
    )
  )
)

################################################################################
# Shiny App Server
################################################################################

server <- function(input, output) {

  # Reactive to get precomputed data based on current inputs
  current_data <- reactive({
    n <- as.numeric(input$n_obs)
    key <- paste0("n", n)
    precomputed_data[[key]]
  })

  output$ciPlot <- renderPlot({
    n <- as.numeric(input$n_obs)
    data <- current_data()$data

    ggplot(data, aes(x = universe)) +
      geom_point(aes(y = lower), color = "blue", alpha = 0.5, size = 0.5) +
      geom_point(aes(y = upper), color = "red", alpha = 0.5, size = 0.5) +
      geom_hline(yintercept = population_mean, linetype = "dashed",
                 color = "black", linewidth = 1) +
      labs(title = sprintf("95%% Confidence Intervals Across %d Universes (n = %d)",
                           n_universes, n),
           subtitle = "Intervals ordered by lower bound for visualization",
           x = "Universe (ordered)",
           y = "Sample Mean Ȳ (minutes)") +
      theme_minimal() +
      theme(plot.title = element_text(size = 14, face = "bold"),
            plot.subtitle = element_text(size = 11))
  })

  output$coverageOutput <- renderText({
    n <- as.numeric(input$n_obs)
    fraction_inside <- current_data()$fraction_inside
    fraction_outside <- current_data()$fraction_outside

    paste0(
      "Coverage: ", sprintf("%.1f%%", fraction_inside * 100),
      " of intervals contain the true μ = ", sprintf("%.2f", population_mean), " minutes\n",
      "(Expected: ~95% for a 95% confidence interval)\n\n",
      "Fraction of universes where μ is OUTSIDE the interval: ",
      sprintf("%.3f", fraction_outside), "\n\n",
      "Sample size: n = ", n, " flights per sample"
    )
  })
}

################################################################################
# Run App
################################################################################

shinyApp(ui = ui, server = server)
