"""Tests for permissions API endpoints."""

import pytest

from vikdata.client import VikdataClient
from vikdata.models.permission import (
    PermissionListParams,
    PermissionListResponse,
)


@pytest.fixture
def mock_permissions():
    """Mock permissions list."""
    return {
        "data": [
            {
                "id": "550e8400-e29b-41d4-a716-446655440000",
                "resource": "users",
                "action": "read",
                "scope": "*",
                "description": "Read all users",
                "service": None,
            },
            {
                "id": "550e8400-e29b-41d4-a716-446655440001",
                "resource": "fishinfo:species",
                "action": "read",
                "scope": "*",
                "description": "Read fish species",
                "service": "fishinfo",
            },
        ],
        "meta": {
            "page": 1,
            "per_page": 100,
            "total": 2,
            "total_pages": 1,
        },
    }


@pytest.mark.asyncio
async def test_list_permissions(respx_mock, mock_permissions):
    """Test listing all permissions."""
    respx_mock.get("http://localhost:8000/api/v1/permissions").mock(
        return_value={"json": mock_permissions, "status_code": 200}
    )

    async with VikdataClient(
        base_url="http://localhost:8000", access_token="test_token"
    ) as client:
        response = await client.permissions.list()

        assert isinstance(response, PermissionListResponse)
        assert len(response.data) == 2
        assert response.data[0].resource == "users"
        assert response.data[1].service == "fishinfo"


@pytest.mark.asyncio
async def test_list_permissions_filtered_by_resource(respx_mock):
    """Test listing permissions filtered by resource."""
    filtered_response = {
        "data": [
            {
                "id": "550e8400-e29b-41d4-a716-446655440000",
                "resource": "users",
                "action": "read",
                "scope": "*",
                "description": "Read all users",
                "service": None,
            }
        ],
        "meta": None,
    }

    respx_mock.get("http://localhost:8000/api/v1/permissions").mock(
        return_value={"json": filtered_response, "status_code": 200}
    )

    async with VikdataClient(
        base_url="http://localhost:8000", access_token="test_token"
    ) as client:
        params = PermissionListParams(resource="users")
        response = await client.permissions.list(params)

        assert isinstance(response, PermissionListResponse)
        assert len(response.data) == 1


@pytest.mark.asyncio
async def test_list_permissions_filtered_by_service(respx_mock):
    """Test listing permissions filtered by service."""
    filtered_response = {
        "data": [
            {
                "id": "550e8400-e29b-41d4-a716-446655440001",
                "resource": "fishinfo:species",
                "action": "read",
                "scope": "*",
                "description": "Read fish species",
                "service": "fishinfo",
            }
        ],
        "meta": None,
    }

    respx_mock.get("http://localhost:8000/api/v1/permissions").mock(
        return_value={"json": filtered_response, "status_code": 200}
    )

    async with VikdataClient(
        base_url="http://localhost:8000", access_token="test_token"
    ) as client:
        params = PermissionListParams(service="fishinfo")
        response = await client.permissions.list(params)

        assert isinstance(response, PermissionListResponse)
        assert len(response.data) == 1
        assert response.data[0].service == "fishinfo"
