#' Read RCL file
#' 
#' @param path RCL file path
#' @return Parsed tibble of RCL file in \code{path}
#' @examples 
#' read_rcg("data/20220405162804-HELIOS_base_3-vs-enemy_2.rcl")
read_rcl <- function(path) {
  rcl <- path |>
  readr::read_lines() |>
  tibble::as_tibble() |>
  dplyr::mutate(
    step     = value |> stringr::str_extract("\\d+") |> as.numeric(),
    agent    = value |> stringr::str_extract("\\w+_([0-9]{1,2}|Coach)(?!\\))"),
    team     = agent |> stringr::str_remove("_([0-9]{1,2}|Coach)"),
    unum     = agent |> stringr::str_extract("([0-9]{1,2}|Coach)$"),
    commands = value |>
      stringr::str_extract("\\(.+\\)$") |>
      purrr::map(~ .x |>
                   stringr::str_split("\\(|\\)", simplify = TRUE) |>
                   stringr::str_trim() |>
                   purrr::discard(~ .x == "")),
  ) |>
  tidyr::unnest(commands) |>
  dplyr::mutate(
    commands = commands |> stringr::str_split("\\ ", n = 2),
    command  = commands |> purrr::map_chr(1),
    args     = commands |> purrr::map(~ .x[-1]),
  ) |>
  dplyr::select(
    step,
    team,
    unum,
    command,
    args,
    # line = value,
  )
  
  return(rcl)
}
