#' @title Directed metadata graph construction for SUNSET's Loading module
#' @description Generate provenance for a SUNSET workflow involving loading of climate forecast, hindcast and observational data.
#' @param recipe Configuration file specifying parameters.
#' @param data s2dv_cube object generated by SUNSET's Loading.R function.
#' @details This function constructs provenance information for a SUNSET workflow, specifically focusing on the loading module. It generates provenance for climate forecast hindcast and observational data, 
#' including dataset subsets and reggriding information. The function integrates other provenance generation functions to create a comprehensive metadata graph.
#' @references SUNSET GitLab Repository: https://earth.bsc.es/gitlab/es/sunset/-/wikis/home
#' This repository hosts SUNSET, a modular tool for subseasonal to seasonal to decadal forecast verification workflows. It is structured modularly to allow skipping or reordering workflow parts.
#' @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/}).

SunsetProv.Dataset <- function(recipe, data) {
  #fcst definition (Dataset+Datasubset)
  if (!is.null(data$fcst)) {
    graph_fcst <- Prov.Dataset(recipe, data, type = "fcst")
    graph_fcst <- Prov.DatasetSubset(recipe, data, graph_fcst)
    graph_fcst <- Prov.Reggriding(recipe, data, graph_fcst)
  }
  #hcst definition (Dataset+Datasubset)
  graph_hcst <- Prov.Dataset(recipe, data, type = "hcst")
  graph_hcst <- Prov.DatasetSubset(recipe, data, graph_hcst)
  graph_hcst <- Prov.Reggriding(recipe, data, graph_hcst)

  #obs definition (Dataset+Datasubset)
  graph_obs <- Prov.Dataset(recipe, data, type = "obs")
  graph_obs <- Prov.DatasetSubset(recipe, data, graph_obs)
  graph_obs <- Prov.Reggriding(recipe, data, graph_obs)

  #Command Call and output definition
  if (!is.null(data$fcst)) {
    graph <- my_union_graph(graph_fcst$graph, graph_hcst$graph, graph_obs$graph)

    graph <- SunsetProv.Command(graph, package = "Loading",
                                version = "2.0.0",
                                fun = "modules/Loading/Loading.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  list(
                                       graph_fcst$parentnodename,
                                       graph_hcst$parentnodename,
                                       graph_obs$parentnodename,
                                       "DatasetSubset_fcst",
                                       "DatasetSubset_hcst",
                                       "DatasetSubset_obs"))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_fcst" = graph_fcst$parentnodename,
                         "parentnodename_hcst" = graph_hcst$parentnodename,
                         "parentnodename_obs" = graph_obs$parentnodename))
  } else {
    graph <- my_union_graph(graph_hcst$graph, graph_obs$graph)

    graph <- SunsetProv.Command(graph, package = "Loading",
                                version = "2.0.0",
                                fun = "modules/Loading/Loading.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  list(
                                       graph_hcst$parentnodename,
                                       graph_obs$parentnodename,
                                       "DatasetSubset_hcst",
                                       "DatasetSubset_obs"))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_hcst" = graph_hcst$parentnodename,
                         "parentnodename_obs" = graph_obs$parentnodename))
  }
  return(graph)
}