#' Merge survival output with provider profile and risk flags
#'
#' @param surv_result Output from vowr_survival()
#' @param profile Output from vowr_profile()
#' @param flagged Output from vowr_flag()
#' @return A provider-level survival analysis data frame
#' @export
vowr_survival_merge <- function(surv_result, profile, flagged) {

  surv_data <- surv_result$surv_data

  profile_keep <- profile[, c(
    "provider_org_code",
    "provider_org_name",
    "n_months",
    "mean_wait",
    "median_wait",
    "volatility",
    "total_backlog"
  )]

  flagged_keep <- flagged[, c(
    "provider_org_code",
    "risk_flag"
  )]

  out <- dplyr::left_join(
    surv_data,
    profile_keep,
    by = "provider_org_code"
  )

  out <- dplyr::left_join(
    out,
    flagged_keep,
    by = "provider_org_code"
  )

  return(out)
}
