#' @title Generate provenance for SUNSET's Scorecards module execution
#' @description Generate provenance information for the execution of SUNSET's Scorecards module.
#' @param recipe Configuration file specifying parameters.
#' @param data s2dv_cube object generated by SUNSET's Scorecards.R function.
#' @return List containing updated graph and parent node name.
#' @details This function generates provenance information for the execution of SUNSET's Scorecards module.
#' @references This function is based on the semantics outlined in the Graphical Output ontology within the Metaclip Framework (available at \url{http://www.metaclip.org/}).
#' @references https://earth.bsc.es/gitlab/es/sunset/-/tree/dev-test-provenance?ref_type=heads Documentation for SUNSET's provenance generation.

SunsetProv.Scorecards <- function(recipe, graph) {

  #Scorecards metadata + parent node names
  parentnodename_fcst <- graph$parentnodename$parentnodename_fcst
  parentnodename_hcst <- graph$parentnodename$parentnodename_hcst
  parentnodename_obs <- graph$parentnodename$parentnodename_obs
  measure <- unlist(strsplit(recipe$Analysis$Workflow$Scorecards$metric, " "))

  #Add Scorecard node
  graph <- graph$graph
  chart.nodename <- "Scorecards_chart"
  graph <- add_vertices(graph,
                        nv = 1,
                        name = chart.nodename,
                        label = "Scorecards",
                        className = "go:Chart")

  if (!is.null(parentnodename_fcst)) {
    for (type in list("fcst", "hcst")) {
      for (names in measure) {
        names <- tolower(names)
        metric.nodename <- paste0("Metrics_", type, "_", names)
        graph <- add_edges(graph,
                           c(getNodeIndexbyName(graph, metric.nodename),
                             getNodeIndexbyName(graph, chart.nodename)),
                           label = "go:hadGraphicalRepresentation") 
      }
    }
  } else {
    type <- "hcst"
    for (names in measure) {
      names <- tolower(names)
      metric.nodename <- paste0("Metrics_", type, "_", names)
      graph <- add_edges(graph,
                         c(getNodeIndexbyName(graph, metric.nodename),
                           getNodeIndexbyName(graph, chart.nodename)),
                         label = "go:hadGraphicalRepresentation")
    }
  }
  #Command Call and output definition
  if (!is.null(data$fcst)) {

    graph <- SunsetProv.Command(graph, package = "Scorecards",
                                version = "2.0.0",
                                fun = "modules/Scorecards/Scorecards.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  list(chart.nodename))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_fcst" = parentnodename_fcst,
                         "parentnodename_hcst" = parentnodename_hcst,
                         "parentnodename_obs" = parentnodename_obs))
  } else {
    graph <- SunsetProv.Command(graph, package = "Scorecards",
                                version = "2.0.0",
                                fun = "modules/Scorecards/Scorecards.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  list(chart.nodename))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_hcst" = parentnodename_hcst,
                         "parentnodename_obs" = parentnodename_obs))
  }

  print(paste0("Scorecards provenance computed"))
  return(graph)
}