"""
Contract tests for monitor API endpoints
These tests define the expected behavior - they will FAIL until implementation is complete
"""
import pytest
from httpx import AsyncClient
from fastapi import status


@pytest.mark.asyncio
class TestMonitorsList:
    """Contract tests for GET /api/v1/monitors"""

    async def test_list_monitors_success_admin(self, async_client: AsyncClient, auth_token: str):
        """Test listing monitors with admin authentication"""
        response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        assert response.status_code == status.HTTP_200_OK
        data = response.json()

        # Verify response structure
        assert "monitors" in data
        assert "total" in data
        assert isinstance(data["monitors"], list)
        assert isinstance(data["total"], int)

        # If monitors exist, verify monitor structure
        if data["monitors"]:
            monitor = data["monitors"][0]
            assert "id" in monitor
            assert "name" in monitor
            assert "description" in monitor
            assert "status" in monitor
            assert "current_camera_id" in monitor

    async def test_list_monitors_success_operator(self, async_client: AsyncClient, operator_token: str):
        """Test listing monitors with operator role"""
        response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {operator_token}"}
        )

        assert response.status_code == status.HTTP_200_OK
        data = response.json()
        assert "monitors" in data

    async def test_list_monitors_success_viewer(self, async_client: AsyncClient, viewer_token: str):
        """Test listing monitors with viewer role (read-only)"""
        response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {viewer_token}"}
        )

        assert response.status_code == status.HTTP_200_OK
        data = response.json()
        assert "monitors" in data

    async def test_list_monitors_no_auth(self, async_client: AsyncClient):
        """Test listing monitors without authentication"""
        response = await async_client.get("/api/v1/monitors")

        assert response.status_code == status.HTTP_401_UNAUTHORIZED
        data = response.json()
        assert "error" in data or "detail" in data

    async def test_list_monitors_invalid_token(self, async_client: AsyncClient):
        """Test listing monitors with invalid token"""
        response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": "Bearer invalid_token_here"}
        )

        assert response.status_code == status.HTTP_401_UNAUTHORIZED

    async def test_list_monitors_expired_token(self, async_client: AsyncClient, expired_token: str):
        """Test listing monitors with expired token"""
        response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {expired_token}"}
        )

        assert response.status_code == status.HTTP_401_UNAUTHORIZED

    async def test_list_monitors_caching(self, async_client: AsyncClient, auth_token: str):
        """Test that monitor list is cached (second request should be faster)"""
        # First request - cache miss
        response1 = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {auth_token}"}
        )
        assert response1.status_code == status.HTTP_200_OK

        # Second request - cache hit
        response2 = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {auth_token}"}
        )
        assert response2.status_code == status.HTTP_200_OK

        # Results should be identical
        assert response1.json() == response2.json()

    async def test_list_monitors_empty_result(self, async_client: AsyncClient, auth_token: str):
        """Test listing monitors when none are available"""
        response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        assert response.status_code == status.HTTP_200_OK
        data = response.json()
        assert "monitors" in data
        assert data["total"] >= 0  # Can be 0 if no monitors


@pytest.mark.asyncio
class TestMonitorDetail:
    """Contract tests for GET /api/v1/monitors/{monitor_id}"""

    async def test_get_monitor_success(self, async_client: AsyncClient, auth_token: str):
        """Test getting single monitor details"""
        # First get list to find a valid monitor ID
        list_response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        monitors = list_response.json()["monitors"]
        if not monitors:
            pytest.skip("No monitors available for testing")

        monitor_id = monitors[0]["id"]

        # Now get monitor detail
        response = await async_client.get(
            f"/api/v1/monitors/{monitor_id}",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        assert response.status_code == status.HTTP_200_OK
        data = response.json()

        # Verify monitor structure
        assert data["id"] == monitor_id
        assert "name" in data
        assert "description" in data
        assert "status" in data
        assert "current_camera_id" in data

    async def test_get_monitor_not_found(self, async_client: AsyncClient, auth_token: str):
        """Test getting non-existent monitor"""
        response = await async_client.get(
            "/api/v1/monitors/99999",  # Non-existent ID
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        assert response.status_code == status.HTTP_404_NOT_FOUND
        data = response.json()
        assert "error" in data or "detail" in data

    async def test_get_monitor_invalid_id(self, async_client: AsyncClient, auth_token: str):
        """Test getting monitor with invalid ID format"""
        response = await async_client.get(
            "/api/v1/monitors/invalid",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Should return 422 (validation error) or 404 (not found)
        assert response.status_code in [status.HTTP_422_UNPROCESSABLE_ENTITY, status.HTTP_404_NOT_FOUND]

    async def test_get_monitor_no_auth(self, async_client: AsyncClient):
        """Test getting monitor without authentication"""
        response = await async_client.get("/api/v1/monitors/1")

        assert response.status_code == status.HTTP_401_UNAUTHORIZED

    async def test_get_monitor_all_roles(self, async_client: AsyncClient, auth_token: str,
                                        operator_token: str, viewer_token: str):
        """Test that all roles can read monitor details"""
        # All roles (viewer, operator, administrator) should be able to read monitors
        for token in [viewer_token, operator_token, auth_token]:
            response = await async_client.get(
                "/api/v1/monitors/1",
                headers={"Authorization": f"Bearer {token}"}
            )
            # Should succeed or return 404 (if monitor doesn't exist), but not 403
            assert response.status_code in [status.HTTP_200_OK, status.HTTP_404_NOT_FOUND]

    async def test_get_monitor_caching(self, async_client: AsyncClient, auth_token: str):
        """Test that monitor details are cached"""
        monitor_id = 1

        # First request - cache miss
        response1 = await async_client.get(
            f"/api/v1/monitors/{monitor_id}",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Second request - cache hit (if monitor exists)
        response2 = await async_client.get(
            f"/api/v1/monitors/{monitor_id}",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Both should have same status code
        assert response1.status_code == response2.status_code

        # If successful, results should be identical
        if response1.status_code == status.HTTP_200_OK:
            assert response1.json() == response2.json()


@pytest.mark.asyncio
class TestMonitorAvailable:
    """Contract tests for GET /api/v1/monitors/filter/available"""

    async def test_get_available_monitors(self, async_client: AsyncClient, auth_token: str):
        """Test getting available (idle/free) monitors"""
        response = await async_client.get(
            "/api/v1/monitors/filter/available",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        assert response.status_code == status.HTTP_200_OK
        data = response.json()

        assert "monitors" in data
        assert "total" in data

        # Available monitors should have no camera assigned (or current_camera_id is None/0)
        if data["monitors"]:
            for monitor in data["monitors"]:
                # Available monitors typically have no camera or camera_id = 0
                assert monitor.get("current_camera_id") is None or monitor.get("current_camera_id") == 0


@pytest.mark.asyncio
class TestMonitorIntegration:
    """Integration tests for monitor endpoints with SDK Bridge"""

    async def test_monitor_data_consistency(self, async_client: AsyncClient, auth_token: str):
        """Test that monitor data is consistent between list and detail endpoints"""
        # Get monitor list
        list_response = await async_client.get(
            "/api/v1/monitors",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        if list_response.status_code != status.HTTP_200_OK:
            pytest.skip("Monitor list not available")

        monitors = list_response.json()["monitors"]
        if not monitors:
            pytest.skip("No monitors available")

        # Get first monitor detail
        monitor_id = monitors[0]["id"]
        detail_response = await async_client.get(
            f"/api/v1/monitors/{monitor_id}",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        assert detail_response.status_code == status.HTTP_200_OK

        # Verify consistency
        list_monitor = monitors[0]
        detail_monitor = detail_response.json()

        assert list_monitor["id"] == detail_monitor["id"]
        assert list_monitor["name"] == detail_monitor["name"]
        assert list_monitor["status"] == detail_monitor["status"]
        assert list_monitor["current_camera_id"] == detail_monitor["current_camera_id"]
