#' Price HDD and CDD Weather Derivatives
#'
#' Evaluates fair values and Monte Carlo standard errors for HDD and CDD Call Options.
#'
#' @param temp_paths Matrix of simulated temperature paths from simulate_weather_paths.
#' @param strike Strike index level K.
#' @param type Option contract type: "HDD" or "CDD".
#' @param r Risk-free discount rate (default 0.04).
#' @param base_temp Threshold index baseline (default 18.0 deg C).
#' @return A list containing estimated price, standard error, and index distribution metrics.
#' @examples
#' paths <- simulate_weather_paths(n_paths = 100, days = 90)
#' hdd_opt <- price_weather_option(paths, strike = 500, type = "HDD")
#' print(hdd_opt$price)
#' @export
price_weather_option <- function(temp_paths, strike, type = c("HDD", "CDD"), r = 0.04, base_temp = 18.0) {
  type <- match.arg(type)
  n_paths <- nrow(temp_paths)
  
  if (type == "HDD") {
    index_val <- rowSums(pmax(base_temp - temp_paths, 0.0))
  } else {
    index_val <- rowSums(pmax(temp_paths - base_temp, 0.0))
  }
  
  payoffs <- exp(-r * 1.0) * pmax(index_val - strike, 0.0)
  price <- mean(payoffs)
  se <- stats::sd(payoffs) / sqrt(n_paths)
  
  list(
    type = type,
    strike = strike,
    price = price,
    standard_error = se,
    mean_index = mean(index_val),
    sd_index = stats::sd(index_val)
  )
}
