from unittest.mock import Mock, patch, MagicMock
from rest_framework.test import APIRequestFactory

from smoothschedule.identity.users.api.views import UserViewSet


class TestUserViewSet:
    """Tests for UserViewSet using mocked users and data."""

    def test_get_queryset(self):
        """Test that get_queryset returns users from the database."""
        # Arrange
        view = UserViewSet()
        request = APIRequestFactory().get("/fake-url/")
        mock_user = Mock()
        mock_user.username = "testuser"
        request.user = mock_user

        view.request = request

        # Mock the queryset
        mock_queryset = MagicMock()
        mock_queryset.__contains__ = Mock(return_value=True)

        with patch.object(view, 'get_queryset', return_value=mock_queryset):
            # Act
            queryset = view.get_queryset()

            # Assert
            assert mock_user in queryset

    def test_me(self):
        """Test that 'me' endpoint returns current user's data."""
        # Arrange
        view = UserViewSet()
        request = APIRequestFactory().get("/fake-url/")

        mock_user = Mock()
        mock_user.username = "testuser"
        mock_user.name = "Test User"
        request.user = mock_user

        view.request = request

        # Mock the serializer
        with patch('smoothschedule.identity.users.api.views.UserSerializer') as mock_serializer:
            mock_serializer_instance = Mock()
            mock_serializer_instance.data = {
                "username": mock_user.username,
                "url": f"http://testserver/api/users/{mock_user.username}/",
                "name": mock_user.name,
            }
            mock_serializer.return_value = mock_serializer_instance

            # Act
            response = view.me(request)  # type: ignore[call-arg, arg-type, misc]

            # Assert
            assert response.data == {
                "username": mock_user.username,
                "url": f"http://testserver/api/users/{mock_user.username}/",
                "name": mock_user.name,
            }
