#' @title Generate provenance for SUNSET's Downscaling module execution
#' @description Generate provenance information for the execution of SUNSET's Downscaling module.
#' @param recipe Configuration file specifying parameters.
#' @param data s2dv_cube object generated by SUNSET's Downscaling.R function.
#' @return List containing updated graph and parent node name.
#' @details This function generates provenance information for the execution of SUNSET's Downscaling module.
#' @references https://earth.bsc.es/gitlab/es/csdownscale CSTools documentation for CSDownscale.
#' @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.Downscaling <- function(recipe, graph){
  #Variable definition
  parentnodename <- graph$parentnodename$parentnodename_hcst
  parentnodename_fcst <- graph$parentnodename$parentnodename_fcst
  parentnodename_obs <- graph$parentnodename$parentnodename_obs
  down <- recipe$Analysis$Workflow$Downscaling$type
  graph <- graph$graph
  archive <- read_yaml("conf/variable-dictionary_provenance.yml")
  if (down %in% names(archive$downscaling)) {
    downsc.method <- archive$downscaling[[down]]$long_name
    downsc.class <- paste0("cal:",archive$downscaling[[down]]$representation)
    description <- archive$downscaling[[down]]$definition
  }else {
    stop("Downscaling method not registered or not properly defined")
  }

  int <- recipe$Analysis$Workflow$Downscaling$int_method

  #Add Downscaling node definition
  downsc.nodename <- paste0("Downscaling_", "hcst")
  graph <- add_vertices(graph,
                        nv = 1,
                        name = downsc.nodename,
                        label=downsc.method,
                        className = downsc.class,
                        attr=
                          list(
                               "dc:description" = description,
                               "dc:comment" = "For more details, see the  
                                CSTools documentation for CSDownscale",
                               "ds:renferenceURL" =
                               "https://earth.bsc.es/gitlab/es/csdownscale"))

  graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, parentnodename),
                       getNodeIndexbyName(graph, downsc.nodename)),
                     label = "cal:hadCalibration")

  #Add New Spatial Extent
  refgraph <- getSpatialExtent(data, type = "hcst", Downsc = TRUE)
  graph <- my_union_graph(graph, refgraph$graph)
  graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, downsc.nodename),
                       getNodeIndexbyName(graph, refgraph$parentnodename)),
                     label = "ds:hasHorizontalExtent")

  #Command Call and output definition
  if (!is.null(data$fcst)) {
    graph <- SunsetProv.Command(graph, package = "Downscaling",
                                version = "2.0.0",
                                fun = "modules/Downscaling/Downscaling.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  list(downsc.nodename))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_fcst" = parentnodename_fcst,
                         "parentnodename_hcst" = downsc.nodename,
                         "parentnodename_obs" = parentnodename_obs))
  } else{
    graph <- SunsetProv.Command(graph, package = "Downscaling",
                                version = "2.0.0",
                                fun = "modules/Downscaling/Downscaling.R",
                                arg.list =
                                  list(
                                       "recipe" = "Recipe",
                                       "Data" = "Data"),
                                origin.node.name =
                                  list(downsc.nodename))
    
    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_hcst" = downsc.nodename,
                         "parentnodename_obs" = parentnodename_obs))
  }
  print(paste0("Downscaling provenance computed for hcst"))

  return(graph)
}