use iri_s::IriS;
use rudof_rdf::rdf_core::{NeighsRDF, SHACLPath, term::Object};
use shacl_ir::compiled::component_ir::ComponentIR;
use shacl_ir::compiled::property_shape::PropertyShapeIR;
use shacl_ir::compiled::shape::ShapeIR;
use shacl_ir::compiled::target::CompiledTarget;
use shacl_ir::schema_ir::SchemaIR;
use shacl_ir::shape_label_idx::ShapeLabelIdx;

use crate::focus_nodes::FocusNodes;
use crate::validate_error::ValidateError;
use crate::validation_report::result::ValidationResult;
use crate::value_nodes::ValueNodes;

pub trait Engine<S: NeighsRDF> {
    /// Pre-builds internal indexes from the data graph for faster target resolution.
    ///
    /// This should be called **once** before the validation loop starts.
    fn build_indexes(&mut self, _store: &S) -> Result<(), Box<ValidateError>> {
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    fn evaluate(
        &mut self,
        store: &S,
        shape: &ShapeIR,
        component: &ComponentIR,
        value_nodes: &ValueNodes<S>,
        source_shape: Option<&ShapeIR>,
        maybe_path: Option<SHACLPath>,
        shapes_graph: &SchemaIR,
    ) -> Result<Vec<ValidationResult>, Box<ValidateError>>;

    fn focus_nodes(&self, store: &S, targets: &[CompiledTarget]) -> Result<FocusNodes<S>, Box<ValidateError>> {
        let targets_iter: Vec<FocusNodes<S>> = targets
            .iter()
            .flat_map(|target| match target {
                CompiledTarget::Node(node) => self.target_node(store, node),
                CompiledTarget::Class(class) => self.target_class(store, class),
                CompiledTarget::SubjectsOf(predicate) => self.target_subject_of(store, predicate),
                CompiledTarget::ObjectsOf(predicate) => self.target_object_of(store, predicate),
                CompiledTarget::ImplicitClass(node) => self.implicit_target_class(store, node),
                CompiledTarget::WrongTargetNode(_) => todo!(),
                CompiledTarget::WrongTargetClass(_) => todo!(),
                CompiledTarget::WrongSubjectsOf(_) => todo!(),
                CompiledTarget::WrongObjectsOf(_) => todo!(),
                CompiledTarget::WrongImplicitClass(_) => todo!(),
            })
            .collect();
        let ts = targets_iter.into_iter().flatten();
        Ok(FocusNodes::from_iter(ts))
    }

    /// If s is a shape in a shapes graph SG and s has value t for sh:targetNode
    /// in SG then { t } is a target from any data graph for s in SG.
    fn target_node(&self, store: &S, node: &Object) -> Result<FocusNodes<S>, Box<ValidateError>>;

    fn target_class(&self, store: &S, class: &Object) -> Result<FocusNodes<S>, Box<ValidateError>>;

    fn target_subject_of(&self, store: &S, predicate: &IriS) -> Result<FocusNodes<S>, Box<ValidateError>>;

    fn target_object_of(&self, store: &S, predicate: &IriS) -> Result<FocusNodes<S>, Box<ValidateError>>;

    fn implicit_target_class(&self, store: &S, shape: &Object) -> Result<FocusNodes<S>, Box<ValidateError>>;

    fn path(
        &self,
        store: &S,
        shape: &PropertyShapeIR,
        focus_node: &S::Term,
    ) -> Result<FocusNodes<S>, Box<ValidateError>> {
        let nodes =
            store
                .objects_for_shacl_path(focus_node, shape.path())
                .map_err(|e| ValidateError::ObjectsSHACLPath {
                    focus_node: focus_node.to_string(),
                    shacl_path: shape.path().to_string(),
                    error: e.to_string(),
                })?;
        Ok(FocusNodes::new(nodes))
    }

    fn record_validation(&mut self, node: Object, shape_idx: ShapeLabelIdx, results: Vec<ValidationResult>);

    fn has_validated(&self, node: &Object, shape_idx: ShapeLabelIdx) -> bool;

    /// Returns the cached validation results for a given `(node, shape_idx)` pair, if any.
    fn get_cached_results(&self, node: &Object, shape_idx: ShapeLabelIdx) -> Option<&Vec<ValidationResult>>;
}
