"""Tests for model download (mocked — no real network)."""

from __future__ import annotations

import pytest


@pytest.mark.unit
class TestModelDownload:
    def test_download_creates_models_dir(self, tmp_path) -> None:
        from llmhosts.models.download import ensure_models_dir

        models_dir = tmp_path / "models"
        ensure_models_dir(str(models_dir))
        assert models_dir.exists()

    def test_is_downloaded_false_when_missing(self, tmp_path) -> None:
        from llmhosts.models.download import is_downloaded

        assert is_downloaded("qwen2.5:7b", models_dir=str(tmp_path)) is False

    def test_is_downloaded_true_when_exists(self, tmp_path) -> None:
        from llmhosts.models.download import is_downloaded

        (tmp_path / "qwen2.5-7b-instruct-q4_k_m.gguf").write_bytes(b"fake")
        assert is_downloaded("qwen2.5:7b", models_dir=str(tmp_path)) is True

    def test_is_downloaded_unknown_model(self, tmp_path) -> None:
        from llmhosts.models.download import is_downloaded

        assert is_downloaded("nonexistent:model", models_dir=str(tmp_path)) is False

    def test_download_unknown_model_raises(self) -> None:
        from llmhosts.models.download import download_model

        with pytest.raises(ValueError, match="Unknown model"):
            download_model("nonexistent:model")

    def test_list_local_models(self, tmp_path) -> None:
        from llmhosts.models.download import list_local_models

        (tmp_path / "qwen2.5-7b-instruct-q4_k_m.gguf").write_bytes(b"fake")
        local = list_local_models(models_dir=str(tmp_path))
        assert len(local) >= 1
        assert any("qwen2.5:7b" in m for m in local)

    def test_list_local_models_empty(self, tmp_path) -> None:
        from llmhosts.models.download import list_local_models

        local = list_local_models(models_dir=str(tmp_path))
        assert local == []

    def test_list_local_models_nonexistent_dir(self, tmp_path) -> None:
        from llmhosts.models.download import list_local_models

        local = list_local_models(models_dir=str(tmp_path / "nonexistent"))
        assert local == []

    def test_download_already_exists(self, tmp_path) -> None:
        from llmhosts.models.download import download_model

        (tmp_path / "qwen2.5-7b-instruct-q4_k_m.gguf").write_bytes(b"fake")
        result = download_model("qwen2.5:7b", models_dir=str(tmp_path))
        assert result.endswith("qwen2.5-7b-instruct-q4_k_m.gguf")

    def test_download_missing_huggingface_hub(self, tmp_path, monkeypatch) -> None:
        import builtins

        real_import = builtins.__import__

        def mock_import(name, *args, **kwargs):
            if name == "huggingface_hub":
                raise ImportError("No module named 'huggingface_hub'")
            return real_import(name, *args, **kwargs)

        monkeypatch.setattr(builtins, "__import__", mock_import)

        from llmhosts.models.download import download_model

        with pytest.raises(RuntimeError, match="huggingface_hub is required"):
            download_model("qwen2.5:7b", models_dir=str(tmp_path))

    def test_list_local_models_multiple(self, tmp_path) -> None:
        from llmhosts.models.download import list_local_models

        (tmp_path / "qwen2.5-7b-instruct-q4_k_m.gguf").write_bytes(b"fake")
        (tmp_path / "qwen2.5-0.5b-instruct-q4_k_m.gguf").write_bytes(b"fake")
        local = list_local_models(models_dir=str(tmp_path))
        assert len(local) == 2
        assert "qwen2.5:7b" in local
        assert "qwen2.5:0.5b" in local
