use std::path::Path;

use eidos_kernel::schema::{Edge, Node};

pub(super) fn skill_wiki_edges(from: &Node, path: &Path, nodes: &[Node]) -> Vec<Edge> {
    let mut edges = Vec::new();
    if path.file_name().is_none_or(|file| file != "SKILL.md") {
        return edges;
    };
    let Some(dir) = path.parent() else {
        return edges;
    };

    for candidate in nodes {
        if candidate.id == from.id {
            continue;
        }
        let owned = candidate.source_files.first().is_some_and(|source| {
            let path = Path::new(source);
            path.starts_with(dir) && path.file_name().is_none_or(|file| file != "SKILL.md")
        });
        if owned {
            edges.push(Edge {
                from: from.id.clone(),
                to: candidate.id.clone(),
                relation: eidos_kernel::schema::relation::HAS_KNOWLEDGE.into(),
                evidence: "skill_wiki".into(),
                // Structural skill→wiki ownership — earned Resolved.
                basis: eidos_kernel::schema::EdgeBasis::Resolved,
                ..Default::default()
            });
        }
    }
    edges
}
