"""Tests for concept graph queries and concept relationship model."""

from __future__ import annotations

from pycharter.wiki.ontology.models import (
    INVERSE_MAP,
    RelationshipType,
    resolve_inverse,
)
from pycharter.wiki.ontology.schema_loader import (
    get_inverse,
    load_knowledge_schema,
    validate_relationship_type,
)


class TestResolveInverseRoundTrip:
    """Test that inverse resolution is consistent across models and schema."""

    def test_inverse_map_covers_all_enum_members(self):
        for rt in RelationshipType:
            assert rt.value in INVERSE_MAP

    def test_resolve_inverse_matches_schema_inverse(self):
        load_knowledge_schema.cache_clear()
        schema = load_knowledge_schema()
        for name, info in schema.relationship_types.items():
            model_inverse = resolve_inverse(name)
            schema_inverse = info.inverse
            # For types in INVERSE_MAP, resolve_inverse uses INVERSE_MAP
            if name in INVERSE_MAP:
                assert model_inverse == INVERSE_MAP[name]
            # get_inverse should also agree
            assert get_inverse(name) == model_inverse

    def test_schema_relationship_types_are_all_valid(self):
        load_knowledge_schema.cache_clear()
        schema = load_knowledge_schema()
        for name in schema.relationship_types:
            assert validate_relationship_type(name) is True


class TestRelationshipTypePredicateMapping:
    """Test that all RelationshipType enum members have JSON-LD predicate mappings."""

    def test_all_types_have_predicates(self):
        from pycharter.wiki.rdf.emitter import _REL_TYPE_TO_PREDICATE

        for rt in RelationshipType:
            assert (
                rt.value in _REL_TYPE_TO_PREDICATE
            ), f"Missing predicate mapping for {rt.value}"

    def test_all_predicates_have_parser_reverse(self):
        from pycharter.wiki.rdf.emitter import _REL_TYPE_TO_PREDICATE
        from pycharter.wiki.rdf.jsonld_parser import _PREDICATE_TO_REL_TYPE

        for rel_type, predicate in _REL_TYPE_TO_PREDICATE.items():
            assert (
                predicate in _PREDICATE_TO_REL_TYPE
            ), f"Predicate '{predicate}' (for {rel_type}) has no parser reverse mapping"
