"""Tests for the review_commands module and helper functions."""

from agentic_devtools.cli.azure_devops.review_helpers import (
    JIRA_ISSUE_KEY_PATTERN,
    extract_jira_issue_key_from_title,
)


class TestExtractJiraIssueKeyFromTitle:
    """Tests for extract_jira_issue_key_from_title function."""

    def test_standard_format(self):
        """Test extraction from standard commit format."""
        title = "feature([PROJECT-1234](https://jira.swica.ch/browse/PROJECT-1234)): add feature"
        result = extract_jira_issue_key_from_title(title)
        assert result == "PROJECT-1234"

    def test_parent_child_format(self):
        """Test extraction returns first match for parent/child format."""
        title = "feature([PROJECT-1840](link) / [PROJECT-1900](link)): description"
        result = extract_jira_issue_key_from_title(title)
        assert result == "PROJECT-1840"

    def test_simple_brackets(self):
        """Test extraction from simple brackets format."""
        title = "[PROJ-999] Fix the bug"
        result = extract_jira_issue_key_from_title(title)
        assert result == "PROJ-999"

    def test_no_jira_key(self):
        """Test returns None when no Jira key present."""
        title = "feature: add feature without ticket"
        result = extract_jira_issue_key_from_title(title)
        assert result is None

    def test_empty_title(self):
        """Test returns None for empty title."""
        result = extract_jira_issue_key_from_title("")
        assert result is None

    def test_none_title(self):
        """Test returns None for None title."""
        result = extract_jira_issue_key_from_title(None)
        assert result is None

    def test_multiple_keys_returns_first(self):
        """Test returns first key when multiple present."""
        title = "ABC-123 DEF-456 XYZ-789"
        result = extract_jira_issue_key_from_title(title)
        assert result == "ABC-123"

    def test_lowercase_does_not_match(self):
        """Test lowercase project keys don't match."""
        title = "project-1234 in lowercase"
        result = extract_jira_issue_key_from_title(title)
        assert result is None


class TestJiraIssueKeyPattern:
    """Tests for the JIRA_ISSUE_KEY_PATTERN regex."""

    def test_pattern_matches_standard_keys(self):
        """Test pattern matches standard Jira keys."""
        matches = JIRA_ISSUE_KEY_PATTERN.findall("PROJECT-1234 PROJ-99 ABC-1")
        assert matches == ["PROJECT-1234", "PROJ-99", "ABC-1"]

    def test_pattern_ignores_lowercase(self):
        """Test pattern ignores lowercase keys."""
        matches = JIRA_ISSUE_KEY_PATTERN.findall("project-1234 proj-99")
        assert matches == []

    def test_pattern_extracts_from_markdown_links(self):
        """Test pattern extracts from markdown links."""
        text = "[PROJECT-1234](https://jira.example.com/browse/PROJECT-1234)"
        matches = JIRA_ISSUE_KEY_PATTERN.findall(text)
        # Should find both occurrences
        assert len(matches) == 2
        assert all(m == "PROJECT-1234" for m in matches)
