"""
Help command integration for transfer JSON-RPC commands.

Author: Vasiliy Zdanovskiy
email: vasilyvz@gmail.com
"""

from __future__ import annotations

import pytest
from fastapi.testclient import TestClient

from mcp_proxy_adapter.api.app import create_app
from mcp_proxy_adapter.commands.transfer_download_begin_command import (
    TransferDownloadBeginCommand,
)
from mcp_proxy_adapter.commands.transfer_download_status_command import (
    TransferDownloadStatusCommand,
)
from mcp_proxy_adapter.commands.transfer_upload_begin_command import (
    TransferUploadBeginCommand,
)
from mcp_proxy_adapter.commands.transfer_upload_complete_command import (
    TransferUploadCompleteCommand,
)
from mcp_proxy_adapter.commands.transfer_upload_status_command import (
    TransferUploadStatusCommand,
)

TRANSFER_NAMES = (
    "transfer_upload_begin",
    "transfer_upload_status",
    "transfer_upload_complete",
    "transfer_download_begin",
    "transfer_download_status",
)

NAME_TO_CLASS = {
    "transfer_upload_begin": TransferUploadBeginCommand,
    "transfer_upload_status": TransferUploadStatusCommand,
    "transfer_upload_complete": TransferUploadCompleteCommand,
    "transfer_download_begin": TransferDownloadBeginCommand,
    "transfer_download_status": TransferDownloadStatusCommand,
}


@pytest.fixture
def client() -> TestClient:
    app = create_app(
        title="Transfer help test",
        description="Integration",
        version="1.0.0",
    )
    return TestClient(app)


def test_help_jsonrpc_lists_all_transfer_commands(client: TestClient) -> None:
    response = client.post(
        "/api/jsonrpc",
        json={"jsonrpc": "2.0", "method": "help", "params": {}, "id": 1},
    )
    assert response.status_code == 200
    data = response.json()
    assert data["result"]["success"] is True
    commands = data["result"]["data"]["commands"]
    for name in TRANSFER_NAMES:
        assert name in commands


@pytest.mark.parametrize("name", TRANSFER_NAMES)
def test_help_jsonrpc_per_command_schema_matches_class_get_schema(
    client: TestClient, name: str
) -> None:
    response = client.post(
        "/api/jsonrpc",
        json={"jsonrpc": "2.0", "method": "help", "params": {"cmdname": name}, "id": 1},
    )
    assert response.status_code == 200
    result = response.json()["result"]
    assert result.get("success") is True
    schema = result["data"]["schema"]
    class_schema = NAME_TO_CLASS[name].get_schema()
    assert set(schema.get("properties", {}).keys()) == set(
        class_schema.get("properties", {}).keys()
    )
    assert schema.get("required", []) == class_schema.get("required", [])
    assert len(schema.get("examples", [])) >= 2


def test_help_jsonrpc_metadata_summary_equals_descr(client: TestClient) -> None:
    for name in TRANSFER_NAMES:
        response = client.post(
            "/api/jsonrpc",
            json={
                "jsonrpc": "2.0",
                "method": "help",
                "params": {"cmdname": name},
                "id": 1,
            },
        )
        assert response.status_code == 200
        result = response.json()["result"]
        assert result["data"]["metadata"]["summary"] == NAME_TO_CLASS[name].descr


def test_transfer_commands_result_schema_direct_from_class() -> None:
    for name in TRANSFER_NAMES:
        cls = NAME_TO_CLASS[name]
        rs = cls.get_result_schema()
        assert isinstance(rs, dict)
        assert rs.get("type") == "object"
