#' Process Structured Interview Data to Extract Interventions
#'
#' This function processes a structured interview (as created by 
#' `prep_interview`) to extract and concatenate the interventions of 
#' each speaker. It removes speaker tags from each segment and then 
#' simplifies the naming of the resulting list elements.
#'
#' @param structured_interview A list where each element is a character 
#'   vector representing an intervention by a specific speaker (as 
#'   returned by `prep_interview`).
#' @param pattern The regular expression pattern used to remove 
#'   speaker tags from the interventions. Typically generated using 
#'   `generate_speaker_pattern`.
#'
#' @return A list where each element is a single string containing 
#'   the concatenated intervention of one speaker, with speaker tags 
#'   removed. The names of the list elements are the unique speaker IDs.
#'
#' @examples
#' # Example Usage:
#' speakers = retrieve_speaker(intrw)
#' pattern = generate_speaker_pattern(speakers)
#' structured_interview = prep_interview(intrw)
#' processed_interview = process_structured_interview(
#'      structured_interview, 
#'      pattern
#' )
#'
#' @family process interview
#' 
#' @export

process_structured_interview = function(structured_interview, pattern){
    result = sapply(
        X = structured_interview,
        FUN = function(x, pattern) {
            unknown = all(grepl("\\[UNKNOWN\\]", x))
            if(isFALSE(unknown)){
                result = concatenate_intervention(
                    intervention = x, 
                    pattern = pattern
                )
            } else {
                result = x
            }
            return(result)
        },
        pattern = pattern,
        simplify = FALSE
    )
    names(result) = gsub(
        "([A-Za-z0-9]+)(_[0-9]+\\.[1-9]+)", 
        replacement = "\\1", 
        names(result)
    )
    return(result)
}