#' Flag high-risk providers
#'
#' @param profile A data frame produced by vowr_profile()
#' @param wait_threshold The mean wait time threshold (default 18 weeks)
#' @param vol_percentile The percentile for volatility (default 0.90)
#' @return The profile data frame with a 'risk_flag' column
#' @export
vowr_flag <- function(profile, wait_threshold = 18, vol_percentile = 0.90) {

  vol_cutoff <- stats::quantile(profile$volatility, vol_percentile, na.rm = TRUE)

  profile$is_breach <- profile$mean_wait > wait_threshold
  profile$is_volatile <- profile$volatility > vol_cutoff

  profile$risk_flag <- dplyr::case_when(
    profile$is_breach & profile$is_volatile ~ "Critical (Breach & Unstable)",
    profile$is_breach                       ~ "High (Breach)",
    profile$is_volatile                     ~ "Warning (Unstable)",
    TRUE                                    ~ "Stable"
  )

  profile <- dplyr::select(profile, -is_breach, -is_volatile)

  return(profile)
}
