from uuid import UUID
from unittest.mock import AsyncMock

import pytest

from dsg_core_client import DSGCoreClient


@pytest.mark.asyncio
async def test_get_item_type_metadata_calls_queries_endpoint() -> None:
    client = DSGCoreClient(base_url="https://example.org", token="token")
    try:
        item_type_id = UUID("00000000-0000-0000-0000-000000000001")
        client.get = AsyncMock(
            return_value={
                "name": "MyType",
                "title": "My Type",
                "ordinal": 1,
                "collectionId": "00000000-0000-0000-0000-000000000002",
                "itemTypeId": str(item_type_id),
                "versions": [
                    {
                        "version": "1.0",
                        "description": "Initial version",
                        "jsonSchema": {"properties": {}},
                    }
                ],
            }
        )  # type: ignore[method-assign]

        response = await client.metadata.get_item_type_metadata(item_type_id)

        assert response.item_type_id == item_type_id
        assert response.versions[0].version == "1.0"
        client.get.assert_awaited_once_with(
            client.path(f"/queries/item-types/{item_type_id}")
        )
    finally:
        await client.close()


@pytest.mark.asyncio
async def test_get_item_type_display_attributes_calls_metadata_endpoint() -> None:
    client = DSGCoreClient(base_url="https://example.org", token="token")
    try:
        item_type_id = UUID("00000000-0000-0000-0000-000000000001")
        client.get = AsyncMock(
            return_value={
                "itemTypeId": str(item_type_id),
                "version": "1.0",
                "displayAttributes": ["name", "code"],
            }
        )  # type: ignore[method-assign]

        response = await client.metadata.get_item_type_display_attributes(
            item_type_id=item_type_id,
            version="1.0",
        )

        assert response.item_type_id == item_type_id
        assert response.display_attributes == ["name", "code"]
        client.get.assert_awaited_once_with(
            client.path(f"/metadata/json-schema/{item_type_id}/display-attributes"),
            params={"version": "1.0"},
        )
    finally:
        await client.close()


@pytest.mark.asyncio
async def test_get_item_type_display_attributes_rejects_empty_version() -> None:
    client = DSGCoreClient(base_url="https://example.org", token="token")
    try:
        item_type_id = UUID("00000000-0000-0000-0000-000000000001")

        with pytest.raises(ValueError, match="version must be a non-empty string"):
            await client.metadata.get_item_type_display_attributes(
                item_type_id=item_type_id,
                version="",
            )
    finally:
        await client.close()
