#' @title Generate provenance for SUNSET command calls
#' @description Generate provenance information for command calls based on metaclipR function metalcipR.graph.Command()
#' @param graph Current provenance graph. 
#' @param package Package name
#' @param version Version name 
#' @param function Function name 
#' @param arg.list Argument list
#' @param origin.node.name List containing the name of the origin nodes to be linked to the command call
#' @return List containing updated graph.
#' @references https://earth.bsc.es/gitlab/es/sunset/-/wikis/home GitLab repository for SUNSET, a modular tool for climate forecast verification workflows.
#' @references This function is based on the semantics outlined in the ontology within the Metaclip Framework (available at \url{http://www.metaclip.org/}).

prov_Command <- function(graph, package, version,
                         fun, arg.list, origin.node.name) {
  
  #Check the inputs are correctly defined
  cmd.node.name <- paste0("Command_", package, version)
  # Argument and ArgumentValue
  time <- Sys.time()
  args <- paste(arg.list, collapse = " ")
  graph <- add_vertices_sunset(graph,
                           name = cmd.node.name,
                           className = "ds:Command",
                           label = fun,
                           attr = list("prov:value" = fun,
                                       "prov:atTime" = as.character(time)))
  
  # Add the edge linking the step with the command
  for (i in 1:length(origin.node.name)) {
    graph <- add_edges(graph,
                       c(
                         getNodeIndexbyName(
                                            graph, origin.node.name[i]),
                         getNodeIndexbyName(
                                            graph, cmd.node.name)),
                         label = "ds:hadCommandCall")
  }

  # Add Arguments (Recipe and Data)
  if ("Recipe" %in% arg.list) {
    recipe.nodename <- paste0("Argument_Recipe_", package)
    graph <- add_vertices_sunset(graph,
                             name = recipe.nodename,
                             className = "ds:Argument",
                             label = "Recipe",
                             attr =
                               list("dc:description" =
                                    "User-defined configuration file",
                                    "prov:AtLocation" = recipe$Run$output_dir))

    graph <- add_edges(graph,
                       c(
                         getNodeIndexbyName(
                                            graph, recipe.nodename),
                         getNodeIndexbyName(
                                            graph, cmd.node.name)),
                       label = "ds:usedArgument")
  }
  
  if ("Data" %in% arg.list) {
    recipe.nodename <- paste0("Argument_Data_", package)
    graph <- add_vertices_sunset(graph,
                             name = recipe.nodename,
                             className = "ds:Argument",
                             label = "Data",
                             attr =
                               list(
                                    "dc:description" =
                                    "s2dv_cube object",
                                    "ds:referenceURL" =
                                    "https://cran.r-project.org/
                                      web/packages/s2dv/index.html",
                                    "dc:comment" = "output s2dv_cube object 
                                      from previous module"))

    graph <- add_edges(graph,
                       c(
                         getNodeIndexbyName(
                                            graph, recipe.nodename),
                         getNodeIndexbyName(
                                            graph, cmd.node.name)),
                       label = "ds:usedArgument")
}


  # Package (SUNSET)
  pkg.nodename <- paste0("Package_", package)
  graph <- add_vertices_sunset(graph = graph,
                            name = pkg.nodename,
                            className = "ds:Package",
                            label = package,
                            attr = list("ds:hasSoftwareVersion" = version,
                                        "dc:isDefinedBy" = "SUNSET",
                                        "dc:comment" = " SUNSET (SUbseasoNal 
                                        to decadal climate forecast 
                                        post-processIng and asSEmenT suite) is
                                        a modular tool for subseasonal 
                                        to seasonal to decadal forecast 
                                        verification workflows. It is 
                                        intended to have a modularized 
                                        structure, where each module is a 
                                        separate part of the code that 
                                        performs a  specific task, so that 
                                        parts of the workflow can be 
                                        skipped or reordered. Each command call 
                                        node corresponds to one of SUNSET's 
                                        modules execution",
                                        "ds:referenceURL" =
                                          "https://earth.bsc.es/gitlab/
                                          es/sunset/"))
  graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, cmd.node.name),
                       getNodeIndexbyName(graph, pkg.nodename)),
                     label = "ds:fromPackage")

  #Retrive git metadata
  #library(git2r)

  #repo <- repository(getwd())  

  #branch <- repository_head(repo)

  #commit <- commits(repo)[[1]]  # Latest commit

  #config <- config(repo)
  #user_name <- config$global$user.name
  #user_email <- config$global$user.email


  # Package (Library versions)
  #pkg.nodename <- paste0("Package_libaries_", randomName())
  #library_versions <- paste(paste0("-", packages[1:10, "Package"],
                                  # "(", packages[1:10, "Version"], ")", "\n"),
                            #collapse = " ")

  #Maybe we should add somwhere the libraries required y sunset...

  #graph <- my_add_vertices(graph = graph,
  #                         name = pkg.nodename,
  #                         className = "ds:Package",
  #                         label = "Libraries used and their Versions",
  #                         attr = list("ds:hasSoftwareVersion" =
  #                                       library_versions))
  #graph <- add_edges(graph,
  #                   c(getNodeIndexbyName(graph, cmd.node.name),
  #                     getNodeIndexbyName(graph, pkg.nodename)),
  #                   label = "ds:fromPackage")

  #Software Agent
  #(...)

  return(graph)
}
