"""Tests for dataset tools."""

from unittest.mock import MagicMock, patch

import pytest

from tests.conftest import MockMCP


@pytest.fixture
def mcp():
    m = MockMCP()
    from kaggle_mcp import datasets
    datasets.register(m)
    return m


# ---------------------------------------------------------------------------
# datasets_list
# ---------------------------------------------------------------------------

def test_datasets_list_returns_formatted(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        d = MagicMock()
        d.title = "Iris Dataset"
        d.ref = "uciml/iris"
        d.total_bytes = 4096
        d.download_count = 500
        mock_gc.return_value.datasets.dataset_api_client.list_datasets.return_value.datasets = [d]

        result = mcp.tools["datasets_list"]()
        assert "Iris Dataset" in result
        assert "uciml/iris" in result


def test_datasets_list_empty(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        mock_gc.return_value.datasets.dataset_api_client.list_datasets.return_value.datasets = []
        result = mcp.tools["datasets_list"]()
        assert result == "No datasets found."


# ---------------------------------------------------------------------------
# dataset_files
# ---------------------------------------------------------------------------

def test_dataset_files_returns_list(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        f = MagicMock()
        f.name = "iris.csv"
        f.total_bytes = 2048
        mock_gc.return_value.datasets.dataset_api_client.list_dataset_files.return_value.files = [f]

        result = mcp.tools["dataset_files"]("uciml", "iris")
        assert "iris.csv" in result


def test_dataset_files_empty(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        mock_gc.return_value.datasets.dataset_api_client.list_dataset_files.return_value.files = []
        result = mcp.tools["dataset_files"]("uciml", "iris")
        assert result == "No files found."


# ---------------------------------------------------------------------------
# dataset_download
# ---------------------------------------------------------------------------

def test_dataset_download_all(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        mock_gc.return_value.datasets.dataset_api_client.download_dataset.return_value.url = "https://example.com/iris.zip"
        result = mcp.tools["dataset_download"]("uciml", "iris")
        assert "https://example.com/iris.zip" in result


def test_dataset_download_single_file(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        mock_gc.return_value.datasets.dataset_api_client.download_dataset.return_value.url = "https://example.com/iris.csv"
        result = mcp.tools["dataset_download"]("uciml", "iris", "iris.csv")
        assert "https://example.com/iris.csv" in result


# ---------------------------------------------------------------------------
# dataset_metadata
# ---------------------------------------------------------------------------

def test_dataset_metadata(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        resp = MagicMock()
        resp.to_dict.return_value = {"title": "Iris", "license": "CC0"}
        mock_gc.return_value.datasets.dataset_api_client.get_dataset_metadata.return_value = resp

        result = mcp.tools["dataset_metadata"]("uciml", "iris")
        assert "Iris" in result or "CC0" in result


# ---------------------------------------------------------------------------
# dataset_create
# ---------------------------------------------------------------------------

def test_dataset_create(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        resp = MagicMock()
        resp.to_dict.return_value = {"ref": "user/my-dataset", "status": "ok"}
        mock_gc.return_value.datasets.dataset_api_client.create_dataset.return_value = resp

        result = mcp.tools["dataset_create"]("user", "my-dataset", "My Dataset")
        assert "my-dataset" in result or "ok" in result


# ---------------------------------------------------------------------------
# file_upload
# ---------------------------------------------------------------------------

def test_file_upload(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc, \
         patch("kaggle_mcp.datasets.requests") as mock_requests:
        resp = MagicMock()
        resp.token = "tok_abc123"
        resp.create_url = "https://storage.example.com/upload"
        mock_gc.return_value.datasets.dataset_api_client.upload_dataset_file.return_value = resp

        result = mcp.tools["file_upload"]("data.csv", "col1,col2\n1,2")
        assert "tok_abc123" in result
        mock_requests.put.assert_called_once()


# ---------------------------------------------------------------------------
# dataset_get
# ---------------------------------------------------------------------------

def test_dataset_get(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        resp = MagicMock()
        resp.to_dict.return_value = {"title": "Iris", "ref": "uciml/iris"}
        mock_gc.return_value.datasets.dataset_api_client.get_dataset.return_value = resp

        result = mcp.tools["dataset_get"]("uciml", "iris")
        assert "Iris" in result or "uciml/iris" in result


def test_dataset_get_error(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        mock_gc.return_value.datasets.dataset_api_client.get_dataset.side_effect = Exception("not found")
        result = mcp.tools["dataset_get"]("bad", "slug")
        assert "Error" in result


# ---------------------------------------------------------------------------
# dataset_create_version
# ---------------------------------------------------------------------------

def test_dataset_create_version(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        resp = MagicMock()
        resp.to_dict.return_value = {"version": 2}
        mock_gc.return_value.datasets.dataset_api_client.create_dataset_version.return_value = resp

        result = mcp.tools["dataset_create_version"]("user", "my-ds", "v2 notes")
        assert "2" in result


# ---------------------------------------------------------------------------
# dataset_update_metadata
# ---------------------------------------------------------------------------

def test_dataset_update_metadata(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        resp = MagicMock()
        resp.to_dict.return_value = {"status": "updated"}
        mock_gc.return_value.datasets.dataset_api_client.update_dataset_metadata.return_value = resp

        result = mcp.tools["dataset_update_metadata"]("user", "my-ds", title="New Title")
        assert "updated" in result or "New Title" in result


# ---------------------------------------------------------------------------
# dataset_delete
# ---------------------------------------------------------------------------

def test_dataset_delete_success(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        resp = MagicMock()
        resp.error = None
        mock_gc.return_value.datasets.dataset_api_client.delete_dataset.return_value = resp

        result = mcp.tools["dataset_delete"]("user", "my-ds")
        assert "deleted" in result.lower()


def test_dataset_delete_error_response(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        resp = MagicMock()
        resp.error = "permission denied"
        mock_gc.return_value.datasets.dataset_api_client.delete_dataset.return_value = resp

        result = mcp.tools["dataset_delete"]("user", "my-ds")
        assert "permission denied" in result


# ---------------------------------------------------------------------------
# dataset_download_file
# ---------------------------------------------------------------------------

def test_dataset_download_file(mcp):
    with patch("kaggle_mcp.datasets.get_client") as mock_gc:
        mock_gc.return_value.datasets.dataset_api_client.download_dataset.return_value.url = "https://example.com/iris.csv"
        result = mcp.tools["dataset_download_file"]("uciml", "iris", "iris.csv")
        assert "https://example.com/iris.csv" in result
