//! # Integrations for Taganak SDK with `clap`
//!
//! This module integrates the Taganak SDK with [clap]. It
//! provides templates for accepting Taganak-related arguments
//! on the command line.

use clap::Args;
use iref::IriBuf;

use taganak_core::sources::SourceError;

use crate::{
    datasets::memory::iridataset::{DatasetFromIriError, IriDataset},
    graphs::{ConfigurableGraph, ConfigurableGraphs, GraphConfigs},
};

#[derive(Args, Debug, Clone)]
#[group(requires = "source_iri")]
pub struct GraphArgsOptional {
    /// IRI to load the graph source from
    #[arg(required = false)]
    pub source_iri: IriBuf,
}

#[derive(Args, Debug, Clone)]
#[group(requires = "source_iri")]
pub struct GraphArgs {
    /// IRI to load the graph source from
    #[arg(required = true)]
    pub source_iri: IriBuf,
}

#[derive(Args, Debug, Clone)]
#[group(requires = "source_iris")]
pub struct DatasetArgs {
    #[arg(required = true)]
    pub source_iris: Vec<IriBuf>,
}

#[derive(Args, Debug, Clone)]
#[group(requires = "source_iris")]
pub struct DatasetArgsOptional {
    #[arg(required = true)]
    pub source_iris: Vec<IriBuf>,
}

impl GraphArgs {
    pub fn graph(&self) -> Result<ConfigurableGraphs, SourceError> {
        let config = GraphConfigs::from_iri(self.source_iri.clone())
            .map_err(|e| SourceError::UnsupportedSourceIri)?;

        ConfigurableGraphs::from_config(config)
    }
}

impl GraphArgsOptional {
    pub fn graph(&self) -> Result<ConfigurableGraphs, SourceError> {
        let config = GraphConfigs::from_iri(self.source_iri.clone())
            .map_err(|e| SourceError::UnsupportedSourceIri)?;

        ConfigurableGraphs::from_config(config)
    }
}

impl DatasetArgs {
    pub async fn dataset(&self) -> Result<IriDataset, DatasetFromIriError> {
        IriDataset::from_iris(&self.source_iris).await
    }
}
impl DatasetArgsOptional {
    pub async fn dataset(&self) -> Result<IriDataset, DatasetFromIriError> {
        IriDataset::from_iris(&self.source_iris).await
    }
}
