"""
T037 — tag normalisation: uppercase → lowercase, whitespace stripped
T038 — tag validation: rejects names > 64 chars, invalid chars
"""

import pytest

from app.repositories.tag_repo import TagRepository


@pytest.mark.parametrize(
    "raw,expected",
    [
        ("Cat", "cat"),
        ("  funny  ", "funny"),
        ("REACTION", "reaction"),
        (" MiXeD ", "mixed"),
    ],
)
def test_normalise_lowercases_and_strips(raw, expected):
    assert TagRepository.normalise(raw) == expected


def test_validate_accepts_valid_tags():
    for name in ["cat", "funny-face", "my_tag", "tag123", "a" * 64]:
        TagRepository.normalise_and_validate(name)  # should not raise


def test_validate_rejects_too_long():
    with pytest.raises(ValueError):
        TagRepository.normalise_and_validate("a" * 65)


def test_validate_rejects_invalid_chars():
    with pytest.raises(ValueError):
        TagRepository.normalise_and_validate("bad tag!")  # space + exclamation


def test_validate_rejects_empty():
    with pytest.raises(ValueError):
        TagRepository.normalise_and_validate("")


def test_validate_applies_normalisation_first():
    # "CAT" normalises to "cat" which is valid
    result = TagRepository.normalise_and_validate("CAT")
    assert result == "cat"
