"""TDD tests for axm_bib.keys — BibTeX key generation. Written BEFORE implementation."""

from __future__ import annotations

from axm_bib.keys import generate_key


class TestGenerateKey:
    """Key format: {first_author_surname}{year}{first_significant_title_word}."""

    def test_basic_key(self) -> None:
        """Standard case: Smith, 2024, 'Deep Learning' → smith2024deep."""
        assert generate_key(["John Smith"], 2024, "Deep Learning") == "smith2024deep"

    def test_multiple_authors_uses_first(self) -> None:
        """Only the first author's surname is used."""
        key = generate_key(["John Smith", "Jane Doe"], 2024, "Deep Learning")
        assert key == "smith2024deep"

    def test_compound_surname(self) -> None:
        """Hyphenated surnames: Jean-Pierre Dupont → dupont."""
        key = generate_key(["Jean-Pierre Dupont"], 2023, "Attention Mechanisms")
        assert key == "dupont2023attention"

    def test_unicode_surname(self) -> None:
        """Unicode characters normalized: José García → garcia."""
        key = generate_key(["José García"], 2024, "Formal Methods")
        assert key == "garcia2024formal"

    def test_diacritics_stripped(self) -> None:
        """Diacritics removed: Müller → muller."""
        key = generate_key(["Hans Müller"], 2024, "Optimal Control")
        assert key == "muller2024optimal"

    def test_no_year(self) -> None:
        """Missing year omitted from key."""
        key = generate_key(["Smith"], None, "Title Word")
        assert key == "smithtitle"

    def test_stopword_skip(self) -> None:
        """Stopwords (a, an, the, of, in, for, ...) are skipped."""
        key = generate_key(["Smith"], 2024, "A Survey of Methods")
        assert key == "smith2024survey"

    def test_single_word_title(self) -> None:
        """Single-word title used directly."""
        key = generate_key(["Smith"], 2024, "Attention")
        assert key == "smith2024attention"

    def test_empty_title(self) -> None:
        """Empty title falls back to 'untitled'."""
        key = generate_key(["Smith"], 2024, "")
        assert key == "smith2024untitled"

    def test_all_stopwords_title(self) -> None:
        """Title of only stopwords falls back to 'untitled'."""
        key = generate_key(["Smith"], 2024, "The In Of")
        assert key == "smith2024untitled"

    def test_suffix_in_name(self) -> None:
        """Author suffixes (Jr., III) stripped."""
        key = generate_key(["Smith Jr."], 2024, "Deep Learning")
        assert key == "smith2024deep"

    def test_deterministic(self) -> None:
        """Same input always produces same output."""
        args = (["John Smith"], 2024, "Deep Learning")
        assert generate_key(*args) == generate_key(*args)

    def test_different_titles_different_keys(self) -> None:
        """Different titles produce different keys."""
        k1 = generate_key(["Smith"], 2024, "Deep Learning")
        k2 = generate_key(["Smith"], 2024, "Reinforcement Learning")
        assert k1 != k2

    def test_lowercase_output(self) -> None:
        """Output is always lowercase."""
        key = generate_key(["SMITH"], 2024, "DEEP LEARNING")
        assert key == key.lower()

    def test_alpha_numeric_only(self) -> None:
        """Output contains only lowercase letters and digits."""
        key = generate_key(["O'Brien"], 2024, "Self-Attention Networks")
        assert key.isalnum()
        assert key == key.lower()

    def test_last_comma_first_format(self) -> None:
        """'Smith, John' format handled (surname first)."""
        key = generate_key(["Smith, John"], 2024, "Deep Learning")
        assert key == "smith2024deep"

    def test_single_name_author(self) -> None:
        """Single-name author (e.g. 'Aristotle')."""
        key = generate_key(["Aristotle"], 2024, "Logic")
        assert key == "aristotle2024logic"
