#' @title Generate provenance for SUNSET's Indices module execution
#' @description Generate provenance information for the execution of SUNSET's Indices module.
#' @param recipe Configuration file specifying parameters.
#' @param data s2dv_cube object generated by SUNSET's Indices.R function.
#' @return List containing updated graph and parent node name.
#' @details This function generates provenance information for the execution of SUNSET's Indices module.
#' @references This function is based on the semantics outlined in the Datasource 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.Indices <- function(recipe, graph) {
  #Indices metadata definitions
  index_name <- names(recipe$Analysis$Workflow$Indices)
  parentnodename_fcst <- graph$parentnodename$parentnodename_fcst
  parentnodename_hcst <- graph$parentnodename$parentnodename_hcst
  parentnodename_obs <- graph$parentnodename$parentnodename_obs
  i <- 0
  graph <- graph$graph

  nodenames <- list()
  indexnodenames <- list()

  for (index in index_name){
    i <- i + 1
    #Match METACLIP class with Index
    class <- switch(index,
                    "NAO" = "ds:NAO",
                    "Nino1+2" = "ds:Nino1plus2",
                    "Nino3" = "ds:Nino3",
                    "Nino3.4" = "ds:Nino3.4",
                    "Nino4" = "ds:Nino4")
    
    #Climate Index type node
    if (index != "NAO"){
      index.nodename <- class
    }else {
      index.nodename <- paste0("ClimateIndex_", i)
    }

    graph <- add_vertices(graph,
                          nv = 1,
                          name = index.nodename,
                          label = index,
                          className = class)

    indexnodenames <- append(indexnodenames, index.nodename)

    for (parentnodename in c(parentnodename_hcst, parentnodename_obs)) {
      type <- strsplit(parentnodename, "_")[[1]][[2]]
      climin.nodename <- paste0("ClimateIndex_", type, "_", i)
      graph <- add_vertices(graph,
                            nv = 1,
                            name = climin.nodename,
                            label = "Climate Index Calculation",
                            className = "ds:ClimateIndexCalculation")

      graph <- add_edges(graph,
                         c(getNodeIndexbyName(graph, parentnodename),
                           getNodeIndexbyName(graph, climin.nodename)),
                         label = "ds:hadClimateIndexCalculation")

      graph <- add_edges(graph,
                         c(getNodeIndexbyName(graph, climin.nodename),
                           getNodeIndexbyName(graph, index.nodename)),
                         label = "ds:withClimateIndex")
                         
      nodenames <- append(nodenames, climin.nodename)
      
    }
  }
  #Command Call and output definition
  if (!is.null(data$fcst)) {
    graph <- SunsetProv.Command(graph, package = "Indices",
                                version = "2.0.0",
                                fun = "modules/Indices/Indices.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  list(nodenames))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_fcst" = parentnodename_fcst,
                         "parentnodename_hcst" = parentnodename_hcst,
                         "parentnodename_obs" = parentnodename_obs,
                         "indicesnodenames" = nodenames))

  } else {
    graph <- SunsetProv.Command(graph, package = "Indices",
                                version = "2.0.0",
                                fun = "modules/Indices/Indices.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  nodenames)
    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_hcst" = parentnodename_hcst,
                         "parentnodename_obs" = parentnodename_obs,
                         "indicesnodenames" = nodenames))
  }
  print(paste0("Indices provenance computed"))
  j <<- NULL
  return(graph)
}
