"""`lookup_institution()` returns full-name keys, not compact snapshot keys.

The on-disk JSON snapshot intentionally uses one-character keys (n, c,
t, …) to save bytes across 200k+ institutions. Callers of the Python
accessor don't touch that file — they get a legible dict.
"""

from __future__ import annotations

import unicodedata

from sqlalchemy import create_engine, select
from sqlalchemy.orm import sessionmaker

from local_deep_research.journal_quality.db import (
    _institution_to_dict,
    _populate_institutions,
)
from local_deep_research.journal_quality.models import (
    Institution,
    JournalQualityBase,
)


def test_institution_to_dict_returns_full_names():
    row = Institution(
        openalex_id="I123",
        name="Test University",
        ror_id="ror-xyz",
        country="NL",
        type="education",
        h_index=42,
        impact_factor=1.5,
        works_count=1000,
        cited_by_count=99999,
    )
    d = _institution_to_dict(row)
    assert set(d) == {
        "name",
        "country",
        "type",
        "h_index",
        "impact_factor",
        "works_count",
        "cited_by_count",
        "ror_id",
    }
    assert d["name"] == "Test University"
    assert d["h_index"] == 42
    assert d["cited_by_count"] == 99999
    assert d["ror_id"] == "ror-xyz"


def test_populate_institutions_nfkc_normalizes_name_lower():
    """Names with NFKC-equivalent compatibility characters must collapse
    to the same name_lower so lookups work across Unicode-equivalent
    forms. The fullwidth ``Ｕｎｉｖｅｒｓｉｔｙ`` and ASCII ``University``
    are NFKC-equivalent and must produce identical name_lower values.
    """
    fullwidth_name = unicodedata.normalize("NFKD", "Ｕｎｉｖｅｒｓｉｔｙ")
    institutions = {
        "I1": {"n": fullwidth_name, "c": "US"},
        "I2": {"n": "University", "c": "GB"},
    }

    engine = create_engine("sqlite:///:memory:")
    try:
        JournalQualityBase.metadata.create_all(engine)
        Session = sessionmaker(bind=engine)
        with Session() as s:
            _populate_institutions(s, institutions)
            s.commit()

        with Session() as s:
            rows = s.scalars(
                select(Institution).order_by(Institution.openalex_id)
            ).all()
            assert [r.name_lower for r in rows] == ["university", "university"]
    finally:
        engine.dispose()
