from uuid import UUID
from unittest.mock import AsyncMock

import pytest

from dsg_core_client import DSGCoreClient


@pytest.mark.asyncio
async def test_query_omits_empty_valid_at() -> None:
    client = DSGCoreClient(base_url="https://example.org", token="token")
    try:
        client.post = AsyncMock(return_value={"count": 0, "data": []})  # type: ignore[method-assign]

        await client.queries.query(
            item_type_id=UUID("00000000-0000-0000-0000-000000000001"),
            valid_at="",
        )

        client.post.assert_awaited_once_with(
            client.path("/queries/items/search"),
            json={
                "itemTypeId": "00000000-0000-0000-0000-000000000001",
                "versions": [],
                "filters": [],
                "filtersMode": "or",
                "orderBy": [],
                "includeHistory": False,
            },
        )
    finally:
        await client.close()


@pytest.mark.asyncio
async def test_query_item_metadata_filter_usage_uses_expected_endpoint() -> None:
    client = DSGCoreClient(base_url="https://example.org", token="token")
    try:
        item_type_id = UUID("00000000-0000-0000-0000-000000000001")
        item_id = UUID("00000000-0000-0000-0000-000000000002")
        client.post = AsyncMock(
            return_value=[
                {
                    "name": "TargetByClcode",
                    "itemTypeId": "00000000-0000-0000-0000-000000000003",
                    "versions": [{"version": "default", "fields": ["clcode"]}],
                }
            ]
        )  # type: ignore[method-assign]

        response = await client.queries.get_item_metadata_filter_usage(
            item_type_id=item_type_id,
            item_id=item_id,
            version="default",
        )

        client.post.assert_awaited_once_with(
            client.path("/queries/items/metadata/filter-usage"),
            json={
                "itemTypeId": "00000000-0000-0000-0000-000000000001",
                "version": "default",
                "itemId": "00000000-0000-0000-0000-000000000002",
            },
        )
        assert response[0].name == "TargetByClcode"
        assert response[0].versions[0].fields == ["clcode"]
    finally:
        await client.close()
