#' @title Directed metadata graph construction for SUNSET's Calibration module
#' @description Generate provenance for a SUNSET's Calibration module execution.
#' @param recipe Configuration file specifying parameters.
#' @param data s2dv_cube object generated by SUNSET's function Calibration.R.
#' @return s2dv_cube object with updated provenance graph.
#' @details This function generates provenance information for the execution of a SUNSET's Calibration module.
#' @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 Calibration ontology within the Metaclip Framework (available at \url{http://www.metaclip.org/}).

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

  #Check if Calibration is requested
  if (recipe$Analysis$Workflow$Calibration$method != "raw") {
    #fcst definition (Dataset+Datasubset)
    if (!is.null(data$fcst)) {
      graph_fcst <- Prov.Calibration(recipe, data, graph, type = "fcst")
    }
    #hcst definition (Dataset+Datasubset)
    graph_hcst <- Prov.Calibration(recipe, data, graph, type = "hcst")

    #obs definition (Dataset+Datasubset)
    graph_obs <- Prov.Calibration(recipe, data, graph, type = "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 = "Calibration",
                                  version = "2.0.0",
                                  fun = "modules/Calibration/Calibration.R",
                                  arg.list =
                                    list(
                                         "recipe" = "Recipe",
                                         "Data" = "Data"),
                                  origin.node.name =
                                    list(
                                        graph_fcst$parentnodename,
                                        graph_hcst$parentnodename))

      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 = "Calibration",
                                  version = "2.0.0",
                                  fun = "modules/Calibration/Calibration.R",
                                  arg.list =
                                    list(
                                         "recipe" = "Recipe",
                                         "Data" = "Data"),
                                  origin.node.name =
                                    list(graph_hcst$parentnodename))

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