"""
Unit tests for platform/tenant_sites/views.py

Tests SiteViewSet, PageViewSet, and public API views.
"""
from unittest.mock import Mock, patch, MagicMock
import pytest
from rest_framework.test import APIRequestFactory


class TestSiteViewSetClass:
    """Tests for SiteViewSet class structure."""

    def test_class_exists(self):
        """Should have SiteViewSet class."""
        from smoothschedule.platform.tenant_sites.views import SiteViewSet

        assert SiteViewSet is not None

    def test_has_serializer_class(self):
        """Should have serializer_class attribute."""
        from smoothschedule.platform.tenant_sites.views import SiteViewSet

        assert hasattr(SiteViewSet, 'serializer_class')

    def test_has_permission_classes(self):
        """Should have permission_classes attribute."""
        from smoothschedule.platform.tenant_sites.views import SiteViewSet

        assert hasattr(SiteViewSet, 'permission_classes')

    def test_has_get_object_method(self):
        """Should have get_object method."""
        from smoothschedule.platform.tenant_sites.views import SiteViewSet

        assert hasattr(SiteViewSet, 'get_object')
        assert callable(SiteViewSet.get_object)

    def test_has_me_action(self):
        """Should have me action."""
        from smoothschedule.platform.tenant_sites.views import SiteViewSet

        assert hasattr(SiteViewSet, 'me')

    def test_has_me_pages_action(self):
        """Should have me_pages action."""
        from smoothschedule.platform.tenant_sites.views import SiteViewSet

        assert hasattr(SiteViewSet, 'me_pages')


class TestPageViewSetClass:
    """Tests for PageViewSet class structure."""

    def test_class_exists(self):
        """Should have PageViewSet class."""
        from smoothschedule.platform.tenant_sites.views import PageViewSet

        assert PageViewSet is not None

    def test_has_serializer_class(self):
        """Should have serializer_class attribute."""
        from smoothschedule.platform.tenant_sites.views import PageViewSet

        assert hasattr(PageViewSet, 'serializer_class')

    def test_has_permission_classes(self):
        """Should have permission_classes attribute."""
        from smoothschedule.platform.tenant_sites.views import PageViewSet

        assert hasattr(PageViewSet, 'permission_classes')

    def test_has_get_queryset_method(self):
        """Should have get_queryset method."""
        from smoothschedule.platform.tenant_sites.views import PageViewSet

        assert hasattr(PageViewSet, 'get_queryset')
        assert callable(PageViewSet.get_queryset)


class TestDomainViewSetClass:
    """Tests for DomainViewSet class structure."""

    def test_class_exists(self):
        """Should have DomainViewSet class."""
        from smoothschedule.platform.tenant_sites.views import DomainViewSet

        assert DomainViewSet is not None

    def test_has_serializer_class(self):
        """Should have serializer_class attribute."""
        from smoothschedule.platform.tenant_sites.views import DomainViewSet

        assert hasattr(DomainViewSet, 'serializer_class')


class TestPublicBusinessInfoView:
    """Tests for PublicBusinessInfoView class."""

    def test_class_exists(self):
        """Should have PublicBusinessInfoView class."""
        from smoothschedule.platform.tenant_sites.views import PublicBusinessInfoView

        assert PublicBusinessInfoView is not None

    def test_allows_any_permission(self):
        """Should allow any user to access."""
        from smoothschedule.platform.tenant_sites.views import PublicBusinessInfoView
        from rest_framework.permissions import AllowAny

        assert AllowAny in PublicBusinessInfoView.permission_classes

    def test_has_get_method(self):
        """Should have get method."""
        from smoothschedule.platform.tenant_sites.views import PublicBusinessInfoView

        assert hasattr(PublicBusinessInfoView, 'get')
        assert callable(PublicBusinessInfoView.get)


class TestPublicPageView:
    """Tests for PublicPageView class."""

    def test_class_exists(self):
        """Should have PublicPageView class."""
        from smoothschedule.platform.tenant_sites.views import PublicPageView

        assert PublicPageView is not None

    def test_allows_any_permission(self):
        """Should allow any user to access."""
        from smoothschedule.platform.tenant_sites.views import PublicPageView
        from rest_framework.permissions import AllowAny

        assert AllowAny in PublicPageView.permission_classes

    def test_has_get_method(self):
        """Should have get method."""
        from smoothschedule.platform.tenant_sites.views import PublicPageView

        assert hasattr(PublicPageView, 'get')
        assert callable(PublicPageView.get)


class TestPublicServiceViewSet:
    """Tests for PublicServiceViewSet class."""

    def test_class_exists(self):
        """Should have PublicServiceViewSet class."""
        from smoothschedule.platform.tenant_sites.views import PublicServiceViewSet

        assert PublicServiceViewSet is not None

    def test_is_readonly_viewset(self):
        """Should be a ReadOnlyModelViewSet."""
        from smoothschedule.platform.tenant_sites.views import PublicServiceViewSet
        from rest_framework.viewsets import ReadOnlyModelViewSet

        assert issubclass(PublicServiceViewSet, ReadOnlyModelViewSet)


class TestPublicBusinessHoursView:
    """Tests for PublicBusinessHoursView class."""

    def test_class_exists(self):
        """Should have PublicBusinessHoursView class."""
        from smoothschedule.platform.tenant_sites.views import PublicBusinessHoursView

        assert PublicBusinessHoursView is not None

    def test_allows_any_permission(self):
        """Should allow any user to access."""
        from smoothschedule.platform.tenant_sites.views import PublicBusinessHoursView
        from rest_framework.permissions import AllowAny

        assert AllowAny in PublicBusinessHoursView.permission_classes


class TestPublicPaymentIntentView:
    """Tests for PublicPaymentIntentView class."""

    def test_class_exists(self):
        """Should have PublicPaymentIntentView class."""
        from smoothschedule.platform.tenant_sites.views import PublicPaymentIntentView

        assert PublicPaymentIntentView is not None


class TestPublicAvailabilityView:
    """Tests for PublicAvailabilityView class."""

    def test_class_exists(self):
        """Should have PublicAvailabilityView class."""
        from smoothschedule.platform.tenant_sites.views import PublicAvailabilityView

        assert PublicAvailabilityView is not None

    def test_allows_any_permission(self):
        """Should allow any user to access."""
        from smoothschedule.platform.tenant_sites.views import PublicAvailabilityView
        from rest_framework.permissions import AllowAny

        assert AllowAny in PublicAvailabilityView.permission_classes


class TestPublicBookingView:
    """Tests for PublicBookingView class."""

    def test_class_exists(self):
        """Should have PublicBookingView class."""
        from smoothschedule.platform.tenant_sites.views import PublicBookingView

        assert PublicBookingView is not None

    def test_allows_any_permission(self):
        """Should allow any user to access."""
        from smoothschedule.platform.tenant_sites.views import PublicBookingView
        from rest_framework.permissions import AllowAny

        assert AllowAny in PublicBookingView.permission_classes

    def test_has_post_method(self):
        """Should have post method for booking."""
        from smoothschedule.platform.tenant_sites.views import PublicBookingView

        assert hasattr(PublicBookingView, 'post')
        assert callable(PublicBookingView.post)


class TestSiteViewSetMe:
    """Tests for SiteViewSet.me action."""

    def test_returns_404_when_site_not_found(self):
        """Should return 404 when site doesn't exist."""
        from smoothschedule.platform.tenant_sites.views import SiteViewSet
        from smoothschedule.platform.tenant_sites.models import Site

        mock_request = Mock()
        mock_request.tenant = Mock(id=1)

        viewset = SiteViewSet()
        viewset.request = mock_request
        viewset.format_kwarg = None

        with patch.object(Site.objects, 'get', side_effect=Site.DoesNotExist()):
            response = viewset.me(mock_request)

        assert response.status_code == 404


class TestPageViewSetGetQueryset:
    """Tests for PageViewSet.get_queryset method."""

    def test_has_get_queryset_method(self):
        """Should have get_queryset method."""
        from smoothschedule.platform.tenant_sites.views import PageViewSet

        viewset = PageViewSet()
        assert hasattr(viewset, 'get_queryset')
        assert callable(viewset.get_queryset)
