# Path: chemtrails/utils.py
# def flatten(sequence):
#     """
#     Flatten an arbitrary nested sequence.
#     Example usage:
#       >> my_list = list(flatten(nested_lists))
#     :param sequence: A nested list or tuple.
#     :returns: A generator with all values in a flat structure.
#     """
#     for i in sequence:
#         if isinstance(i, Sequence) and not isinstance(i, (str, bytes)):
#             yield from flatten(i)
#         else:
#             yield i


from collections import defaultdict
from django.core.exceptions import ValidationError
from libcypher_parser import parse_statement
from neomodel import db
from chemtrails.utils import flatten
# -*- coding: utf-8 -*-





def get_relationship_types():
    """
    :returns: A flat list of all relationship types in the database.
    :rtype: list
    """
    query = 'MATCH (n)-[r]-() RETURN DISTINCT type(r) ORDER BY (type(r))'
    validate_cypher(query, raise_exception=True)
    result, _ = db.cypher_query(query)
    return list(flatten(result))


def get_node_permissions():
    """
    :returns: A distinct list of all available permissions.
    :rtype: list
    """
    query = 'MATCH (n)-[r]-() WHERE n.type = \'MetaNode\' RETURN DISTINCT n.default_permissions'
    validate_cypher(query, raise_exception=True)
    result, _ = db.cypher_query(query)

# Path: chemtrails/utils.py
# def flatten(sequence):
#     """
#     Flatten an arbitrary nested sequence.
#     Example usage:
#       >> my_list = list(flatten(nested_lists))
#     :param sequence: A nested list or tuple.
#     :returns: A generator with all values in a flat structure.
#     """
#     for i in sequence:
#         if isinstance(i, Sequence) and not isinstance(i, (str, bytes)):
#             yield from flatten(i)
#         else:
#             yield i


# Path: chemtrails/neoutils/query.py
from collections import defaultdict
from django.core.exceptions import ValidationError
from libcypher_parser import parse_statement
from neomodel import db
from chemtrails.utils import flatten

# -*- coding: utf-8 -*-





def get_relationship_types():
    """
    :returns: A flat list of all relationship types in the database.
    :rtype: list
    """
    query = 'MATCH (n)-[r]-() RETURN DISTINCT type(r) ORDER BY (type(r))'
    validate_cypher(query, raise_exception=True)
    result, _ = db.cypher_query(query)
    return list(flatten(result))


def get_node_permissions():
    """
    :returns: A distinct list of all available permissions.
    :rtype: list
    """
    query = 'MATCH (n)-[r]-() WHERE n.type = \'MetaNode\' RETURN DISTINCT n.default_permissions'
    validate_cypher(query, raise_exception=True)
    result, _ = db.cypher_query(query)

    return list(flatten(result))