"""Unit tests for the remote device registry loader."""

from __future__ import annotations

from textwrap import dedent
from typing import TYPE_CHECKING

import pytest

from tests.remote.devices import DeviceSpec, get_device, load_registry

if TYPE_CHECKING:
    from pathlib import Path


@pytest.fixture()
def sample_toml(tmp_path: Path) -> Path:
    """Create a minimal devices.toml for testing."""
    content = dedent("""\
        [test-box]
        hostname = "test.local"
        username = "tester"
        shell = "bash"
        os = "linux"
        gpu = "RTX 3090"
        ram_gb = 64
        ssh_key = "~/.ssh/test_key"
        description = "Test device"

        [win-box]
        hostname = "win.local"
        username = "admin"
        shell = "powershell"
        os = "windows"
        gpu = "RTX 4090"
        ram_gb = 32
        port = 2222
    """)
    toml_file = tmp_path / "devices.toml"
    toml_file.write_text(content)
    return toml_file


class TestLoadRegistry:
    def test_loads_all_devices(self, sample_toml: Path) -> None:
        registry = load_registry(sample_toml)
        assert len(registry) == 2
        assert "test-box" in registry
        assert "win-box" in registry

    def test_device_fields(self, sample_toml: Path) -> None:
        registry = load_registry(sample_toml)
        dev = registry["test-box"]
        assert dev.hostname == "test.local"
        assert dev.username == "tester"
        assert dev.shell == "bash"
        assert dev.os == "linux"
        assert dev.gpu == "RTX 3090"
        assert dev.ram_gb == 64
        assert dev.ssh_key == "~/.ssh/test_key"
        assert dev.description == "Test device"
        assert dev.port == 22  # Default

    def test_custom_port(self, sample_toml: Path) -> None:
        registry = load_registry(sample_toml)
        dev = registry["win-box"]
        assert dev.port == 2222

    def test_optional_fields_default(self, sample_toml: Path) -> None:
        registry = load_registry(sample_toml)
        dev = registry["win-box"]
        assert dev.ssh_key is None
        assert dev.description == ""

    def test_file_not_found(self, tmp_path: Path) -> None:
        with pytest.raises(FileNotFoundError):
            load_registry(tmp_path / "nonexistent.toml")

    def test_loads_bundled_registry(self) -> None:
        """Verify the actual devices.toml in the repo loads correctly."""
        registry = load_registry()
        assert len(registry) >= 3  # alienware, mac-mini, dual-4090
        assert "alienware" in registry
        assert "mac-mini" in registry
        assert "dual-4090" in registry


class TestGetDevice:
    def test_returns_device(self, sample_toml: Path) -> None:
        dev = get_device("test-box", sample_toml)
        assert isinstance(dev, DeviceSpec)
        assert dev.name == "test-box"

    def test_raises_for_unknown(self, sample_toml: Path) -> None:
        with pytest.raises(KeyError, match="nonexistent"):
            get_device("nonexistent", sample_toml)


class TestDeviceInfoConversion:
    def test_to_device_info(self, sample_toml: Path) -> None:
        spec = get_device("test-box", sample_toml)
        info = spec.to_device_info()
        assert info.hostname == "test.local"
        assert info.username == "tester"
        assert info.shell == "bash"
        assert info.os == "linux"
        assert info.ssh_key == "~/.ssh/test_key"
        assert info.port == 22
