#' @title Generate provenance for SUNSET's Anomalies module execution
#' @description Generate provenance information for the execution of SUNSET's Anomalies module. Returns an igraph object with the appended provenance. 
#' @param recipe Configuration file specifying parameters (SUNSET recipe).
#' @param data s2dv_cube object generated by SUNSET's Anomalies.R function.
#' @param graph Current provenance graph. 
#' @param type Type of data: "fcst" for forecast, "hcst" for hindcast, or "obs" for observational data.
#' @return List containing updated graph and parent node name.
#' @details This function generates provenance information for the execution of SUNSET's Anomalies module.
#' @references This function is based on the semantics outlined in the Calibration 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.
#' @import igraph

Prov.Anomalies<-function(recipe, data, graph, type){
  #Define parentnodename and Anomalies provenance metadata
  i <- switch(type,
              "fcst" = "parentnodename_fcst",
              "hcst" = "parentnodename_hcst",
              "obs" = "parentnodename_obs")
  parentnodename <- graph$parentnodename[[i]]
  graph <- graph$graph
  dc.description <- "Anomaly calculation in climatology involves subtracting
                  a climatological baseline from observed or modeled data 
                  to isolate deviations from the average conditions. 
                  This process, often augmented by cross-validation techniques
                  , allows for the identification and analysis of unusual or 
                  abnormal climate patterns.For more details, see the  CSTools 
                  documentation for CST_Anomaly()"

  referenceURL <- "https://cran.r-project.org/web/packages/CSTools/CSTools.pdf"

  if (recipe$Analysis$Workflow$Anomalies$compute) {
    #Add climatology node("obs" and "hcst"), for "fcst" the "hcst"
    #climatology node is computed to use it as reference.
    if (type == "obs" || type == "hcst") {
      clim.nodename <- paste0("Climatology_", type)

      graph <- add_vertices(graph,
                            nv = 1,
                            name = clim.nodename,
                            label = "Climatology",
                            className = "ds:Climatology")

      graph <- add_edges(graph,
                         c(getNodeIndexbyName(graph, parentnodename),
                           getNodeIndexbyName(graph, clim.nodename)),
                         label = "ds:hadClimatology")
    }else {
      clim.nodename <- "Climatology_hcst"
      graph <- add_vertices(graph,
                            nv = 1,
                            name = clim.nodename,
                            label = "Climatology",
                            className = "ds:Climatology"
      )
    }

    #Add Anomaly calculation provenance
    anom.nodename <- paste0("Anomalies_", type)

    if (recipe$Analysis$Workflow$Anomalies$cross_validation) {
      comment <- "Cross-validation computed. Cross-validation enhances 
                  the robustness of anomaly calculations by validating the 
                  chosen baseline against independent datasets, ensuring 
                  greater reliability in assessing climate variability and 
                  change. Anomalies thus derived are valuable for assessing 
                  trends, detecting extremes, and informing decision-making in 
                  various fields such as agriculture, water resource management
                  , and disaster preparedness."

      graph <- add_vertices(graph,
                              nv = 1,
                              name = anom.nodename,
                              label = "Anomaly",
                              className = "ds:Anomaly",
                              attr = list("dc:description" = dc.description,
                                          "dc:comment" = comment,
                                          "ds:referenceURL" = referenceURL))

    }else{
      graph <- add_vertices(graph,
                            nv = 1,
                            name = anom.nodename,
                            label = "Anomaly",
                            className = "ds:Anomaly",
                            attr = list("dc:description" = dc.description,
                              "ds:referenceURL" = referenceURL
                            ))
    }
    graph <- add_edges(graph,
                       c(getNodeIndexbyName(graph, parentnodename),
                         getNodeIndexbyName(graph, anom.nodename)),
                       label = "ds:hadAnomalyCalculation")

    graph <- add_edges(graph,
                       c(getNodeIndexbyName(graph, anom.nodename),
                         getNodeIndexbyName(graph, clim.nodename)),
                       label = "ds:withReference")

    print(paste0("Anomalies provenance computed for ", type))
    return(list("graph" = graph, "parentnodename" = anom.nodename))
  }else {
    return(list("graph" = graph$graph, "parentnodename" = parentnodename))
  }
}