"""
Tests for pull_request_details_commands module.

Covers:
- Dry-run and validation tests for get_pull_request_details
- Contribution API parsing (_get_viewed_files_via_contribution)
- Iteration change tracking (_get_iteration_change_tracking_map)
- Reviewer payload building (_get_reviewer_payload)
"""

from unittest.mock import patch


class TestGetChangeTrackingIdForFile:
    """Tests for get_change_tracking_id_for_file function."""

    def test_returns_change_tracking_id_when_file_found(self):
        """Should return changeTrackingId when file is found."""
        from agentic_devtools.cli.azure_devops.pull_request_details_commands import (
            get_change_tracking_id_for_file,
        )

        mock_changes = [
            {"changeTrackingId": 42, "item": {"path": "/src/file.py"}},
            {"changeTrackingId": 43, "item": {"path": "/src/other.py"}},
        ]
        with patch(
            "agentic_devtools.cli.azure_devops.pull_request_details_commands._get_iteration_changes",
            return_value=mock_changes,
        ):
            result = get_change_tracking_id_for_file(
                "https://dev.azure.com/org", "project", "repo-id", 123, 1, "/src/file.py", {}
            )

        assert result == 42

    def test_normalizes_path_without_leading_slash(self):
        """Should find file even if input path lacks leading slash."""
        from agentic_devtools.cli.azure_devops.pull_request_details_commands import (
            get_change_tracking_id_for_file,
        )

        mock_changes = [{"changeTrackingId": 42, "item": {"path": "/src/file.py"}}]
        with patch(
            "agentic_devtools.cli.azure_devops.pull_request_details_commands._get_iteration_changes",
            return_value=mock_changes,
        ):
            result = get_change_tracking_id_for_file(
                "https://dev.azure.com/org",
                "project",
                "repo-id",
                123,
                1,
                "src/file.py",
                {},  # No leading slash
            )

        assert result == 42

    def test_returns_none_when_file_not_found(self):
        """Should return None when file is not in changes."""
        from agentic_devtools.cli.azure_devops.pull_request_details_commands import (
            get_change_tracking_id_for_file,
        )

        mock_changes = [{"changeTrackingId": 42, "item": {"path": "/src/other.py"}}]
        with patch(
            "agentic_devtools.cli.azure_devops.pull_request_details_commands._get_iteration_changes",
            return_value=mock_changes,
        ):
            result = get_change_tracking_id_for_file(
                "https://dev.azure.com/org", "project", "repo-id", 123, 1, "/src/notfound.py", {}
            )

        assert result is None

    def test_returns_none_when_changes_api_fails(self):
        """Should return None when getting changes fails."""
        from agentic_devtools.cli.azure_devops.pull_request_details_commands import (
            get_change_tracking_id_for_file,
        )

        with patch(
            "agentic_devtools.cli.azure_devops.pull_request_details_commands._get_iteration_changes",
            return_value=None,
        ):
            result = get_change_tracking_id_for_file(
                "https://dev.azure.com/org", "project", "repo-id", 123, 1, "/src/file.py", {}
            )

        assert result is None
