"""Tests for the sandbox sub-resource."""

from __future__ import annotations

import httpx
import pytest

import kanoniv
from kanoniv.exceptions import ValidationError


class TestSandboxStatus:
    def test_get_status(self, mock_api, client):
        mock_api.get("/v1/sandbox/status").mock(
            return_value=httpx.Response(
                200,
                json={
                    "is_sandbox": True,
                    "entity_count": 247,
                    "entity_limit": 1000,
                    "source_count": 3,
                },
            )
        )
        result = client.sandbox.status()
        assert result["is_sandbox"] is True
        assert result["entity_count"] == 247
        assert result["entity_limit"] == 1000
        assert result["source_count"] == 3

    def test_status_not_sandbox(self, mock_api, client):
        mock_api.get("/v1/sandbox/status").mock(
            return_value=httpx.Response(
                400,
                json={"detail": "This endpoint is only available for sandbox tenants."},
            )
        )
        with pytest.raises(ValidationError):
            client.sandbox.status()


class TestSandboxReset:
    def test_reset(self, mock_api, client):
        mock_api.post("/v1/sandbox/reset").mock(
            return_value=httpx.Response(
                200,
                json={
                    "message": "Sandbox data cleared successfully.",
                    "entities_deleted": 247,
                },
            )
        )
        result = client.sandbox.reset()
        assert result["message"] == "Sandbox data cleared successfully."
        assert result["entities_deleted"] == 247

    def test_reset_not_sandbox(self, mock_api, client):
        mock_api.post("/v1/sandbox/reset").mock(
            return_value=httpx.Response(
                400,
                json={"detail": "This endpoint is only available for sandbox tenants."},
            )
        )
        with pytest.raises(ValidationError):
            client.sandbox.reset()

    def test_reset_empty_sandbox(self, mock_api, client):
        mock_api.post("/v1/sandbox/reset").mock(
            return_value=httpx.Response(
                200,
                json={
                    "message": "Sandbox data cleared successfully.",
                    "entities_deleted": 0,
                },
            )
        )
        result = client.sandbox.reset()
        assert result["entities_deleted"] == 0


class TestSandboxAsync:
    @pytest.mark.asyncio
    async def test_async_status(self, mock_api, async_client):
        mock_api.get("/v1/sandbox/status").mock(
            return_value=httpx.Response(
                200,
                json={
                    "is_sandbox": True,
                    "entity_count": 100,
                    "entity_limit": 1000,
                    "source_count": 2,
                },
            )
        )
        result = await async_client.sandbox.status()
        assert result["is_sandbox"] is True
        assert result["entity_count"] == 100
        await async_client.close()

    @pytest.mark.asyncio
    async def test_async_reset(self, mock_api, async_client):
        mock_api.post("/v1/sandbox/reset").mock(
            return_value=httpx.Response(
                200,
                json={
                    "message": "Sandbox data cleared successfully.",
                    "entities_deleted": 100,
                },
            )
        )
        result = await async_client.sandbox.reset()
        assert result["entities_deleted"] == 100
        await async_client.close()
