"""Clean suite — wipe device to clean state before testing."""

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 CleanSuite(BaseSuite):
    name = "clean"
    description = "Remove llmhosts installation and config to start fresh"

    async def execute(self, runner: RemoteTestRunner, result: SuiteResult) -> None:
        # Step 1: Uninstall pip package
        cmd = "pip uninstall llmhosts -y"
        r = await runner.run_command(cmd, timeout=120.0)
        # exit_code != 0 is OK if not installed
        self._add_step(
            result,
            "uninstall_pip",
            "Uninstall llmhosts pip package",
            status=StepStatus.PASSED,
            stdout=r.stdout,
            stderr=r.stderr,
            duration_s=r.duration_s,
        )

        # Step 2: Remove config directory
        if runner.device.os == "windows":
            config_rm = 'if (Test-Path "$env:USERPROFILE\\.llmhosts") { Remove-Item -Recurse -Force "$env:USERPROFILE\\.llmhosts" }'
        else:
            config_rm = "rm -rf ~/.llmhosts ~/.config/llmhosts"

        r = await runner.run_command(config_rm, timeout=30.0)
        self._add_step(
            result,
            "remove_config",
            "Remove config and data directories",
            status=StepStatus.PASSED if r.success else StepStatus.FAILED,
            stdout=r.stdout,
            stderr=r.stderr,
            duration_s=r.duration_s,
        )

        # Step 3: Verify clean state
        r = await runner.run_command("llmhosts --version", timeout=15.0)
        if r.exit_code != 0:
            self._add_step(
                result,
                "verify_clean",
                "Verify llmhosts command is not available",
                status=StepStatus.PASSED,
                stdout="Command not found (expected)",
                duration_s=r.duration_s,
            )
        else:
            self._add_step(
                result,
                "verify_clean",
                "Verify llmhosts command is not available",
                status=StepStatus.FAILED,
                stdout=r.stdout,
                error_message="llmhosts is still installed after cleanup",
                duration_s=r.duration_s,
            )
