#' @title Directed metadata graph construction for SUNSET's loaded Dataset.
#' @description Generate provenance for the loaded Dataset within a
#'              SUNSET workflow.
#' @param recipe Configuration file specifying parameters.
#' @param data s2dv_cube object generated by SUNSET's Loading.R function.
#' @param type Type of data: "fcst" for forecast, "hcst" for hindcast,
#'             or "obs" for observational data.
#' @details This function constructs provenance information for the
#'          loaded Dataset within a SUNSET workflow.It creates nodes and
#'          edges representing various aspects of the Dataset, including
#'          its subclass, data provider, modeling center, and project
#'          information.
#' @references This function is based on the semantics outlined
#'             in the Datasource 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. # nolint: line_length_linter.
#' @import igraph

Prov.Dataset <- function(recipe, data, type) {
  #First metadata definitions
  Horizon <- tolower(recipe$Analysis$Horizon) #Either seasonal or decadal
  System.name <- recipe$Analysis$Datasets$System$name #System name
  Reference.name <- recipe$Analysis$Datasets$Reference$name #Reference name

  #Dataset setting (extract metadata from conf/ file)
  if (Horizon == "seasonal") {
    archive <- read_yaml("conf/archive.yml")
    if (System.name %in% names(archive$esarchive$System) &&
          (Reference.name %in% names(archive$esarchive$Reference))) {
      archive_system <- archive$esarchive$System[[System.name]]
      archive_reference <- archive$esarchive$Reference[[Reference.name]]
    }else if (System.name %in% names(archive$mars$System)) {
      archive_system <- archive$mars$System[[System.name]]
      archive_reference <- archive$mars$Reference[[Reference.name]]
    }
  }else if (Horizon == "decadal") {
    archive<-read_yaml("conf/archive_decadal.yml")
    if (System.name %in% names(archive$esarchive$System) &&
          (Reference.name %in% names(archive$esarchive$Reference))) {
      archive_system <- archive$esarchive$System[[System.name]]
      archive_reference <- archive$esarchive$Reference[[Reference.name]]
    }
  }else {
    print("The System or Reference datasource is not registered")
  }

  #Set vocabulary Classes
  if (type == "fcst" || type == "hcst") {
    Dataset.subclass <- "SeasonalOperationalForecast"
    ModellingCenter <- archive_system$name
    fullname <- archive_system$institution

    if (ModellingCenter == "ECMWF SEAS5" || 
          ModellingCenter == "ECMWF SEAS5 (v5.1)") {
      ModellingCenter.nodename <- "ds:ECMWF"
      ModellingCenter <- "ECMWF"
    }else {
      ModellingCenter.nodename <- paste0("ModellingCenter")
    }
  }else if (type == "obs") {
    Dataset.subclass <- "ObservationalDataset"
    ModellingCenter <- archive_reference$name
    fullname <- archive_reference$institution

    if (ModellingCenter == "ERA5" || ModellingCenter == "ERA5-Land") {
      ModellingCenter.nodename <- "ds:ERA5"
      ModellingCenter <- "ERA5"
    }else if (ModellingCenter == "ECMWF UERRA" ||
                ModellingCenter == "ECMWF CERRA" || ModellingCenter ==
                "ECMWF CERRA-Land") {
      ModellingCenter.nodename <- "ds:ECMWF"
      ModellingCenter <- "ECMWF"
    }else {
      ModellingCenter.nodename <- paste0("ModellingCenter_", type)
    }
  }
  #"MultiDecadalSimulation can be included in the future"

  #Create the main graph
  graph <- make_empty_graph(0)

  #Add Dataset node
  Dataset.nodename <- paste0("Dataset_", type)
  graph <- my_add_vertices(graph,
                           nv = 1,
                           name = Dataset.nodename,
                           label = "Dataset",
                           className = paste0("ds:", Dataset.subclass))
  #Add Data Provider
  DataProvider <- "CDS"
  DataProvider.nodename <- "ds:CDS"
  graph <- my_add_vertices(graph,
                           name = DataProvider.nodename,
                           label = DataProvider,
                           className = "ds:DataProvider")
  graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, Dataset.nodename),
                       getNodeIndexbyName(graph, DataProvider.nodename)),
                     label = "ds:hadDataProvider")

  #Add Modelling Center
  graph <- my_add_vertices(graph,
                           nv = 1,
                           name = ModellingCenter.nodename,
                           label = ModellingCenter,
                           className = "ds:ModellingCenter",
                           attr = list("dc:title" = fullname))
  graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, Dataset.nodename),
                       getNodeIndexbyName(graph, ModellingCenter.nodename)),
                     label = "ds:hadModellingCenter")
  #Add Project
  Project.nodename <- paste0("Project_", type)
  graph <- my_add_vertices(graph,
                           nv = 1,
                           name = "ds:Copernicus",
                           label = "Copernicus",
                           className = "ds:Project")

  graph <- add_edges(graph,
                     c(getNodeIndexbyName(graph, Dataset.nodename),
                       getNodeIndexbyName(graph, "ds:Copernicus")),
                     label = "ds:hadProject")

  return(list("graph" = graph, "parentnodename" = Dataset.nodename))
}
