package org.studiorailgun.ai.conversation.web;

import java.util.List;
import java.util.stream.Collectors;

import org.studiorailgun.Globals;
import org.studiorailgun.ai.knowledge.Node;
import org.studiorailgun.ai.knowledge.Relation;
import org.studiorailgun.ai.knowledge.types.RelationTypes;

/**
 * Queries about meta information about nodes
 */
public class MetaQuery {
    
    /**
     * Flags a node as learnable
     * @param node The node
     */
    public static void flagAsLearnable(Node node){
        Node learnable = Globals.web.getAnchors().getLearnableNode();
        Globals.web.createRelation(RelationTypes.QUALITY_OF, learnable, node);
    }

    /**
     * Checks if this is a learnable node
     * @param node The node
     * @return true if it is learnable, false otherwise
     */
    public static boolean isLearnable(Node node){
        Node learnableNode = Globals.web.getAnchors().getLearnableNode();
        List<Relation> relations = Globals.web.getRelationsOfChildNode(node);
        List<Node> discoveredLearnable = relations.stream()
            .filter(relation -> relation.getName().equals(RelationTypes.QUALITY_OF))
            .map(relation -> relation.getParent())
            .filter(parentNode -> parentNode.equals(learnableNode))
            .collect(Collectors.toList());
        return discoveredLearnable.size() > 0;
    }

    /**
     * Equates two nodes
     * @param node1 The first node
     * @param node2 The second node
     */
    public static void equate(Node node1, Node node2){
        Globals.web.createRelation(RelationTypes.EQUALS, node1, node2);
        Globals.web.createRelation(RelationTypes.EQUALS, node2, node1);
    }


}
