#' @title Generate provenance for Skill computation
#' @description Generate provenance information for Skill module in SUNSET based on forecast verification.
#' @param recipe Configuration file specifying parameters.
#' @param data s2dv_cube object generated by SUNSET's Loading.R function.
#' @param graph Graph object containing provenance information.
#' @param type Type of data: "fcst" for forecast, "hcst" for hindcast, or "obs" for observational data.
#' @return List containing updated graph and parent node name.
#' @details This function generates provenance information for the computation of skill based on forecast verification.
#' @import igraph
#' @references https://earth.bsc.es/gitlab/es/sunset/-/tree/dev-test-provenance?ref_type=heads Documentation for SUNSET's provenance generation.
#' ç#' @references This function is based on the semantics outlined in the Verification ontology within the Metaclip Framework (available at \url{http://www.metaclip.org/}).

Prov.Skill <- function(recipe, data, graph, type) {
  #Define parameters
  i <- switch(type,
              "fcst" = "parentnodename_fcst",
              "hcst" = "parentnodename_hcst",
              "obs" = "parentnodename_obs")

  #Particular case of computing Skill metrics for several Indices
  parentnodename <- graph$parentnodename[[i]]
  reference.nodename <- graph$parentnodename$parentnodename_obs
  graph <- graph$graph
  measure <- recipe$Analysis$Workflow$Skill$metric

  #Change the nodename of the Verification step in
  #case this is not the only verification step!!

  if (type == "fcst" || type == "hcst") {
    #Verification node
    verification.nodename <- paste0("Verification_", type)
    graph <- add_vertices(graph,
                          nv = 1,
                          name = verification.nodename,
                          label = "Verification",
                          className = "veri:ForecastVerification")

    #Linking the verification
    graph <- add_edges(graph,
                       c(getNodeIndexbyName(graph, parentnodename),
                         getNodeIndexbyName(graph, verification.nodename)),
                       label = "veri:hadValidation")

    #Linking the verification with the observations
    graph <- add_edges(graph,
                       c(getNodeIndexbyName(graph, reference.nodename),
                         getNodeIndexbyName(graph, verification.nodename)),
                       label = "veri:withReference")

    #Add Verification metrics
    measure <- unlist(strsplit(measure, " "))
    archive <- read_yaml("conf/variable-dictionary_provenance.yml")
    for (names in measure) {
      metric <- tolower(names)
      if (metric %in% names(archive$metrics)) {
        metric.method <- archive$metrics[[metric]]$long_name
        metric.class <- paste0("veri:",
                               archive$metrics[[metric]]$representation)
        description <- archive$metrics[[metric]]$definition
      }else {
        stop("Skill method not registered or not properly defined")
      }
      metric.nodename <- paste0("Metrics_", type, "_", metric)
      graph <- add_vertices(graph,
                            nv = 1,
                            name = metric.nodename,
                            label = metric.method,
                            className = metric.class,
                            attr = list("dc:description" = description))

      graph <- add_edges(graph,
                         c(getNodeIndexbyName(graph, metric.nodename),
                           getNodeIndexbyName(graph, verification.nodename)),
                         label = "veri:withForecastRepresentation")
    }
    print(paste0("Skill provenance computed for ", type))
    return(list("graph" = graph, "parentnodename" = verification.nodename))
  }else{
    print(paste0("Skill provenance computed for ", type))
    return(list("graph" = graph, "parentnodename" = parentnodename))
  }
}