"""Install suite — install llmhosts from PyPI and verify."""

from __future__ import annotations

from typing import TYPE_CHECKING

from tests.remote.suites.base import BaseSuite, StepStatus, SuiteResult

if TYPE_CHECKING:
    from tests.remote.runner import RemoteTestRunner


class InstallSuite(BaseSuite):
    name = "install"
    description = "Install llmhosts from PyPI and verify basic functionality"

    async def execute(self, runner: RemoteTestRunner, result: SuiteResult) -> None:
        # Step 1: Install from PyPI
        r = await runner.run_command("pip install llmhosts", timeout=300.0)
        if not r.success:
            self._add_step(
                result,
                "pip_install",
                "Install llmhosts from PyPI",
                status=StepStatus.FAILED,
                stdout=r.stdout,
                stderr=r.stderr,
                duration_s=r.duration_s,
                error_message=f"pip install failed with exit code {r.exit_code}",
            )
            return

        self._add_step(
            result,
            "pip_install",
            "Install llmhosts from PyPI",
            status=StepStatus.PASSED,
            stdout=r.stdout,
            duration_s=r.duration_s,
        )

        # Step 2: Verify version
        r = await runner.run_command("llmhosts --version", timeout=15.0)
        if r.success and r.stdout:
            self._add_step(
                result,
                "verify_version",
                f"Verify version output: {r.stdout}",
                status=StepStatus.PASSED,
                stdout=r.stdout,
                duration_s=r.duration_s,
            )
        else:
            self._add_step(
                result,
                "verify_version",
                "Verify llmhosts --version",
                status=StepStatus.FAILED,
                stdout=r.stdout,
                stderr=r.stderr,
                duration_s=r.duration_s,
                error_message="Version command failed or produced no output",
            )

        # Step 3: Verify help
        r = await runner.run_command("llmhosts --help", timeout=15.0)
        if r.success and "Usage" in r.stdout:
            self._add_step(
                result,
                "verify_help",
                "Verify llmhosts --help shows usage",
                status=StepStatus.PASSED,
                stdout=r.stdout[:500],  # Truncate for readability
                duration_s=r.duration_s,
            )
        else:
            self._add_step(
                result,
                "verify_help",
                "Verify llmhosts --help",
                status=StepStatus.FAILED,
                stdout=r.stdout,
                stderr=r.stderr,
                duration_s=r.duration_s,
                error_message="Help command failed or missing Usage text",
            )
