"""Docker wheel selection testing utilities."""

from __future__ import annotations

import logging
from pathlib import Path
from typing import TYPE_CHECKING, Any

from aida_mcp.utils.command_runner import run_command

if TYPE_CHECKING:
    from mcp.server.fastmcp import Context as MCPContext

    Context = MCPContext[Any, Any, Any]

logger = logging.getLogger(__name__)


async def test_wheel_selection(workspace_root: Path, service_dir: str) -> tuple[bool, str]:
    """
    Quick test of wheel selection logic for tabular-exporter (catches issues in seconds).

    Args:
        workspace_root: Workspace root path
        service_dir: Service directory path

    Returns:
        Tuple of (passed, message)
    """
    if "tabular-exporter" not in service_dir:
        return True, ""  # Only relevant for tabular-exporter

    test_script = workspace_root / service_dir / "test-wheel-selection.sh"
    if not test_script.exists():
        logger.warning(f"Wheel selection test script not found: {test_script}")
        return True, ""  # Not a failure, just skip if script doesn't exist

    logger.info(f"Running quick wheel selection test for {service_dir}")
    cmd_result = await run_command(["bash", str(test_script)], workspace_root, timeout=30, ctx=None, step_name=None)
    exit_code = cmd_result.exit_code
    stdout = cmd_result.stdout
    stderr = cmd_result.stderr

    if exit_code == 0:
        return True, "✅ Wheel selection logic validated (quick test passed)"
    else:
        return False, f"❌ Wheel selection logic failed:\n{stdout}\n{stderr}"
