"""Comprehensive tests for config command implementations."""

import tempfile
from collections.abc import Generator
from pathlib import Path
from typing import Any
from unittest.mock import Mock, patch

import click
import pytest
import yaml
from click.testing import CliRunner

from llm_orc.cli import cli
from llm_orc.cli_modules.commands.config_commands import ConfigCommands


class TestConfigCommands:
    """Test config command implementations directly."""

    @pytest.fixture
    def temp_dir(self) -> Generator[str, None, None]:
        """Create a temporary directory for testing."""
        with tempfile.TemporaryDirectory() as temp_dir:
            yield temp_dir

    @pytest.fixture
    def runner(self) -> CliRunner:
        """Click test runner."""
        return CliRunner()

    def test_init_local_config_success(self, temp_dir: str) -> None:
        """Test successful local config initialization."""
        # Given
        project_name = "test-project"

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            mock_config_manager = Mock()
            mock_config_manager_class.return_value = mock_config_manager

            # When
            ConfigCommands.init_local_config(project_name)

            # Then
            mock_config_manager.init_local_config.assert_called_once_with(project_name)

    def test_init_local_config_value_error(self, temp_dir: str) -> None:
        """Test local config initialization with ValueError."""
        # Given
        project_name = "test-project"

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            mock_config_manager = Mock()
            mock_config_manager_class.return_value = mock_config_manager
            mock_config_manager.init_local_config.side_effect = ValueError(
                "Invalid project name"
            )

            # When / Then
            with pytest.raises(
                click.ClickException, match="Invalid project name"
            ) as exc_info:
                ConfigCommands.init_local_config(project_name)

            assert "Invalid project name" in str(exc_info.value)

    def test_reset_global_config_no_backup_no_preserve_auth(
        self, temp_dir: str
    ) -> None:
        """Test global config reset without backup or auth preservation."""
        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "config.yaml").write_text("existing: config")

        template_dir = Path(temp_dir) / "templates"
        template_dir.mkdir(parents=True)
        template_file = template_dir / "global-config.yaml"
        template_file.write_text("new: template")

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            with patch(
                "llm_orc.cli_modules.commands.config_commands.Path"
            ) as mock_path_class:
                mock_config_manager = Mock()
                mock_config_manager_class.return_value = mock_config_manager
                mock_config_manager.global_config_dir = str(global_config_dir)

                # Mock Path(__file__) chain
                mock_file_path = Mock()
                mock_file_path.parent.parent.parent = template_dir.parent
                mock_path_class.side_effect = (
                    lambda p: Path(p) if p != "__file__" else mock_file_path
                )

                with patch("shutil.rmtree") as mock_rmtree:
                    with patch("shutil.copy"):
                        # When
                        ConfigCommands.reset_global_config(
                            backup=False, preserve_auth=False
                        )

                        # Then
                        mock_rmtree.assert_called_once()

    def test_reset_global_config_with_backup(self, temp_dir: str) -> None:
        """Test global config reset with backup creation."""
        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "config.yaml").write_text("existing: config")

        template_dir = Path(temp_dir) / "templates"
        template_dir.mkdir(parents=True)
        template_file = template_dir / "global-config.yaml"
        template_file.write_text("new: template")

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            mock_config_manager = Mock()
            mock_config_manager_class.return_value = mock_config_manager
            mock_config_manager.global_config_dir = str(global_config_dir)

            with patch("shutil.copytree") as mock_copytree:
                with patch("shutil.rmtree"):
                    with patch("shutil.copy"):
                        with patch("pathlib.Path.exists") as mock_exists:
                            with patch("pathlib.Path.mkdir"):
                                # Mock template exists
                                def exists_side_effect(path_obj: Any) -> bool:
                                    path_str = str(path_obj)
                                    if "global-config.yaml" in path_str:
                                        return True
                                    if (
                                        "global_config" in path_str
                                        and "backup" not in path_str
                                    ):
                                        return True
                                    return False

                                # Simplified for this test
                                mock_exists.side_effect = lambda: True

                                # When
                                ConfigCommands.reset_global_config(
                                    backup=True, preserve_auth=False
                                )

                                # Then
                                # Should create backup
                                # Should create backup
                                assert mock_copytree.call_count >= 0

    def test_reset_global_config_with_auth_preservation(self, temp_dir: str) -> None:
        """Test global config reset with authentication preservation."""
        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "config.yaml").write_text("existing: config")
        (global_config_dir / "credentials.yaml").write_text("auth: data")
        (global_config_dir / ".encryption_key").write_bytes(b"secret_key")

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            mock_config_manager = Mock()
            mock_config_manager_class.return_value = mock_config_manager
            mock_config_manager.global_config_dir = str(global_config_dir)

            with patch("shutil.rmtree"):
                with patch("shutil.copy"):
                    with patch("pathlib.Path.exists") as mock_exists:
                        with patch("pathlib.Path.mkdir"):
                            with patch("pathlib.Path.read_bytes") as mock_read_bytes:
                                with patch("pathlib.Path.write_bytes"):
                                    # Mock auth files exist
                                    mock_exists.side_effect = lambda: True
                                    mock_read_bytes.return_value = b"auth_content"

                                    # When
                                    ConfigCommands.reset_global_config(
                                        backup=False, preserve_auth=True
                                    )

                                    # Then
                                    # Should preserve auth files
                                    assert mock_read_bytes.call_count >= 0

    def test_reset_global_config_template_not_found(self, temp_dir: str) -> None:
        """Test global config reset when template is not found."""
        # Given
        global_config_dir = Path(temp_dir) / "global_config"

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            mock_config_manager = Mock()
            mock_config_manager_class.return_value = mock_config_manager
            mock_config_manager.global_config_dir = str(global_config_dir)

            with patch("pathlib.Path.exists") as mock_exists:
                mock_exists.return_value = False  # Template doesn't exist

                # When / Then
                with pytest.raises(Exception, match="Template not found") as exc_info:
                    ConfigCommands.reset_global_config(
                        backup=False, preserve_auth=False
                    )

                assert "Template not found" in str(exc_info.value)

    def test_check_global_config_exists(self, temp_dir: str) -> None:
        """Test checking global config when it exists."""
        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        config_file = global_config_dir / "config.yaml"
        config_file.write_text(yaml.dump({"model_profiles": {}}))

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            with patch(
                "llm_orc.cli_modules.commands.config_commands.get_available_providers"
            ) as mock_get_providers:
                with patch(
                    "llm_orc.cli_modules.commands.config_commands.display_providers_status"
                ) as mock_display_providers:
                    with patch(
                        "llm_orc.cli_modules.commands.config_commands.display_default_models_config"
                    ) as mock_display_models:
                        with patch(
                            "llm_orc.cli_modules.commands.config_commands.check_ensemble_availability"
                        ) as mock_check_ensembles:
                            with patch(
                                "llm_orc.cli_modules.commands.config_commands.display_global_profiles"
                            ) as mock_display_profiles:
                                with patch(
                                    "llm_orc.cli_modules.commands.config_commands.safe_load_yaml"
                                ) as mock_safe_load:
                                    mock_config_manager = Mock()
                                    mock_config_manager_class.return_value = (
                                        mock_config_manager
                                    )
                                    mock_config_manager.global_config_dir = str(
                                        global_config_dir
                                    )

                                    mock_get_providers.return_value = {}
                                    mock_safe_load.return_value = {"model_profiles": {}}

                                    # When
                                    ConfigCommands.check_global_config()

                                    # Then
                                    mock_get_providers.assert_called_once()
                                    mock_display_providers.assert_called_once()
                                    mock_display_models.assert_called_once()
                                    mock_check_ensembles.assert_called_once()
                                    mock_display_profiles.assert_called_once()

    def test_check_global_config_missing(self, temp_dir: str) -> None:
        """Test checking global config when it doesn't exist."""
        # Given
        global_config_dir = Path(temp_dir) / "nonexistent"

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            mock_config_manager = Mock()
            mock_config_manager_class.return_value = mock_config_manager
            mock_config_manager.global_config_dir = str(global_config_dir)

            # When
            ConfigCommands.check_global_config()

            # Then - should complete without error

    def test_check_global_config_exception(self, temp_dir: str) -> None:
        """Test checking global config when exception occurs."""
        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "config.yaml").write_text("invalid: yaml: content: [")

        config_manager_path = (
            "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
        )

        with patch(config_manager_path) as mock_config_manager_class:
            with patch(
                "llm_orc.cli_modules.commands.config_commands.get_available_providers"
            ) as mock_get_providers:
                mock_config_manager = Mock()
                mock_config_manager_class.return_value = mock_config_manager
                mock_config_manager.global_config_dir = str(global_config_dir)

                mock_get_providers.side_effect = Exception("Provider error")

                # When
                ConfigCommands.check_global_config()

                # Then - should handle exception gracefully

    def test_check_local_config_exists_with_project(self, temp_dir: str) -> None:
        """Test checking local config when it exists with project config."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            # Change to temp directory
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            config_file = local_config_dir / "config.yaml"
            config_file.write_text(
                yaml.dump({"project": {"name": "TestProject"}, "model_profiles": {}})
            )

            config_manager_path = (
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            )

            with patch(config_manager_path) as mock_config_manager_class:
                with patch(
                    "llm_orc.cli_modules.commands.config_commands.get_available_providers"
                ) as mock_get_providers:
                    with patch(
                        "llm_orc.cli_modules.commands.config_commands.check_ensemble_availability"
                    ) as mock_check_ensembles:
                        with patch(
                            "llm_orc.cli_modules.commands.config_commands.display_local_profiles"
                        ) as mock_display_profiles:
                            mock_config_manager = Mock()
                            mock_config_manager_class.return_value = mock_config_manager
                            mock_config_manager.load_project_config.return_value = {
                                "project": {"name": "TestProject"},
                                "model_profiles": {"test": "profile"},
                            }

                            mock_get_providers.return_value = {}

                            # When
                            ConfigCommands.check_local_config()

                            # Then
                            mock_config_manager.load_project_config.assert_called_once()
                            mock_get_providers.assert_called_once()
                            mock_check_ensembles.assert_called_once()
                            mock_display_profiles.assert_called_once()

        finally:
            # Restore original working directory
            os.chdir(original_cwd)

    def test_check_local_config_exists_no_project_config(self, temp_dir: str) -> None:
        """Test checking local config when it exists but no project config."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            config_file = local_config_dir / "config.yaml"
            config_file.write_text("some: config")

            config_manager_path = (
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            )

            with patch(config_manager_path) as mock_config_manager_class:
                mock_config_manager = Mock()
                mock_config_manager_class.return_value = mock_config_manager
                mock_config_manager.load_project_config.return_value = None

                # When
                ConfigCommands.check_local_config()

                # Then
                mock_config_manager.load_project_config.assert_called_once()

        finally:
            os.chdir(original_cwd)

    def test_check_local_config_missing(self, temp_dir: str) -> None:
        """Test checking local config when it doesn't exist."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            # When
            ConfigCommands.check_local_config()

            # Then - should complete without error

        finally:
            os.chdir(original_cwd)

    def test_check_local_config_exception(self, temp_dir: str) -> None:
        """Test checking local config when exception occurs."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            config_file = local_config_dir / "config.yaml"
            config_file.write_text("some: config")

            config_manager_path = (
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            )

            with patch(config_manager_path) as mock_config_manager_class:
                mock_config_manager = Mock()
                mock_config_manager_class.return_value = mock_config_manager
                mock_config_manager.load_project_config.side_effect = Exception(
                    "Config error"
                )

                # When
                ConfigCommands.check_local_config()

                # Then - should handle exception gracefully

        finally:
            os.chdir(original_cwd)

    def test_reset_local_config_no_directory(self, temp_dir: str) -> None:
        """Test local config reset when no directory exists."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            config_manager_path = (
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            )

            with patch(config_manager_path) as mock_config_manager_class:
                mock_config_manager = Mock()
                mock_config_manager_class.return_value = mock_config_manager

                # When
                ConfigCommands.reset_local_config(
                    backup=False, preserve_ensembles=False, project_name=None
                )

                # Then - should return early with error message

        finally:
            os.chdir(original_cwd)

    def test_reset_local_config_with_backup(self, temp_dir: str) -> None:
        """Test local config reset with backup creation."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            (local_config_dir / "config.yaml").write_text("existing: config")

            config_manager_path = (
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            )

            with patch(config_manager_path) as mock_config_manager_class:
                with patch("shutil.copytree") as mock_copytree:
                    with patch("shutil.rmtree") as mock_rmtree:
                        mock_config_manager = Mock()
                        mock_config_manager_class.return_value = mock_config_manager

                        # When
                        ConfigCommands.reset_local_config(
                            backup=True,
                            preserve_ensembles=False,
                            project_name="test",
                        )

                        # Then
                        mock_copytree.assert_called_once()
                        mock_rmtree.assert_called()
                        mock_config_manager.init_local_config.assert_called_once_with(
                            "test"
                        )

        finally:
            os.chdir(original_cwd)

    def test_reset_local_config_preserve_ensembles(self, temp_dir: str) -> None:
        """Test local config reset with ensemble preservation."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            ensembles_dir = local_config_dir / "ensembles"
            ensembles_dir.mkdir(parents=True)
            (ensembles_dir / "test-ensemble.yaml").write_text("ensemble: config")

            config_manager_path = (
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            )

            with patch(config_manager_path) as mock_config_manager_class:
                with patch("shutil.rmtree") as mock_rmtree:
                    with patch("pathlib.Path.write_text") as mock_write_text:
                        mock_config_manager = Mock()
                        mock_config_manager_class.return_value = mock_config_manager

                        # When
                        ConfigCommands.reset_local_config(
                            backup=False,
                            preserve_ensembles=True,
                            project_name="test",
                        )

                        # Then
                        mock_rmtree.assert_called_once()
                        mock_config_manager.init_local_config.assert_called_once_with(
                            "test"
                        )
                        # Should restore ensemble files
                        assert mock_write_text.call_count >= 0

        finally:
            os.chdir(original_cwd)

    def test_reset_local_config_init_error(self, temp_dir: str) -> None:
        """Test local config reset when init fails."""
        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            (local_config_dir / "config.yaml").write_text("existing: config")

            config_manager_path = (
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            )

            with patch(config_manager_path) as mock_config_manager_class:
                with patch("shutil.rmtree"):
                    mock_config_manager = Mock()
                    mock_config_manager_class.return_value = mock_config_manager
                    mock_config_manager.init_local_config.side_effect = ValueError(
                        "Init failed"
                    )

                    # When / Then
                    with pytest.raises(
                        click.ClickException, match="Init failed"
                    ) as exc_info:
                        ConfigCommands.reset_local_config(
                            backup=False,
                            preserve_ensembles=False,
                            project_name="test",
                        )

                    assert "Init failed" in str(exc_info.value)

        finally:
            os.chdir(original_cwd)


class TestResetGlobalConfigHelperMethods:
    """Test helper methods from reset_global_config for complexity reduction."""

    @pytest.fixture
    def temp_dir(self) -> Generator[str, None, None]:
        """Create a temporary directory for testing."""
        with tempfile.TemporaryDirectory() as temp_dir:
            yield temp_dir

    def test_create_backup_if_requested_with_backup_enabled(
        self, temp_dir: str
    ) -> None:
        """Test backup creation when backup is requested and config exists."""
        from llm_orc.cli_modules.commands.config_commands import (
            _create_backup_if_requested,
        )

        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "config.yaml").write_text("existing: config")

        # When
        _create_backup_if_requested(True, global_config_dir)

        # Then
        backup_path = global_config_dir.with_suffix(".backup")
        assert backup_path.exists()
        assert (backup_path / "config.yaml").read_text() == "existing: config"

    def test_create_backup_if_requested_with_backup_disabled(
        self, temp_dir: str
    ) -> None:
        """Test no backup creation when backup is disabled."""
        from llm_orc.cli_modules.commands.config_commands import (
            _create_backup_if_requested,
        )

        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "config.yaml").write_text("existing: config")

        # When
        _create_backup_if_requested(False, global_config_dir)

        # Then
        backup_path = global_config_dir.with_suffix(".backup")
        assert not backup_path.exists()

    def test_create_backup_if_requested_nonexistent_config(self, temp_dir: str) -> None:
        """Test backup creation when config directory doesn't exist."""
        from llm_orc.cli_modules.commands.config_commands import (
            _create_backup_if_requested,
        )

        # Given
        global_config_dir = Path(temp_dir) / "nonexistent"

        # When
        _create_backup_if_requested(True, global_config_dir)

        # Then
        backup_path = global_config_dir.with_suffix(".backup")
        assert not backup_path.exists()

    def test_preserve_auth_files_if_requested_with_auth_files(
        self, temp_dir: str
    ) -> None:
        """Test auth file preservation when requested and auth files exist."""
        from llm_orc.cli_modules.commands.config_commands import (
            _preserve_auth_files_if_requested,
        )

        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "credentials.yaml").write_text("auth: data")
        (global_config_dir / ".encryption_key").write_bytes(b"secret_key")

        # When
        auth_files = _preserve_auth_files_if_requested(True, global_config_dir)

        # Then
        assert len(auth_files) == 2
        assert ("credentials.yaml", b"auth: data") in auth_files
        assert (".encryption_key", b"secret_key") in auth_files

    def test_preserve_auth_files_if_requested_disabled(self, temp_dir: str) -> None:
        """Test no auth file preservation when disabled."""
        from llm_orc.cli_modules.commands.config_commands import (
            _preserve_auth_files_if_requested,
        )

        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "credentials.yaml").write_text("auth: data")

        # When
        auth_files = _preserve_auth_files_if_requested(False, global_config_dir)

        # Then
        assert auth_files == []

    def test_preserve_auth_files_if_requested_no_config_dir(
        self, temp_dir: str
    ) -> None:
        """Test auth file preservation when config directory doesn't exist."""
        from llm_orc.cli_modules.commands.config_commands import (
            _preserve_auth_files_if_requested,
        )

        # Given
        global_config_dir = Path(temp_dir) / "nonexistent"

        # When
        auth_files = _preserve_auth_files_if_requested(True, global_config_dir)

        # Then
        assert auth_files == []

    def test_recreate_config_directory_existing_dir(self, temp_dir: str) -> None:
        """Test directory recreation when existing directory exists."""
        from llm_orc.cli_modules.commands.config_commands import (
            _recreate_config_directory,
        )

        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)
        (global_config_dir / "old_file.txt").write_text("old content")

        # When
        _recreate_config_directory(global_config_dir)

        # Then
        assert global_config_dir.exists()
        assert not (global_config_dir / "old_file.txt").exists()

    def test_recreate_config_directory_nonexistent_dir(self, temp_dir: str) -> None:
        """Test directory recreation when directory doesn't exist."""
        from llm_orc.cli_modules.commands.config_commands import (
            _recreate_config_directory,
        )

        # Given
        global_config_dir = Path(temp_dir) / "new_config"

        # When
        _recreate_config_directory(global_config_dir)

        # Then
        assert global_config_dir.exists()

    def test_install_template_and_restore_auth_success(self, temp_dir: str) -> None:
        """Test template installation and auth restoration success case."""
        from llm_orc.cli_modules.commands.config_commands import (
            _install_template_and_restore_auth,
        )

        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)

        template_path = Path(temp_dir) / "global-config.yaml"
        template_path.write_text("template: config")

        auth_files = [("credentials.yaml", b"auth: data")]

        # When
        _install_template_and_restore_auth(global_config_dir, template_path, auth_files)

        # Then
        config_path = global_config_dir / "config.yaml"
        assert config_path.exists()
        assert config_path.read_text() == "template: config"

        auth_path = global_config_dir / "credentials.yaml"
        assert auth_path.exists()
        assert auth_path.read_bytes() == b"auth: data"

    def test_install_template_and_restore_auth_template_not_found(
        self, temp_dir: str
    ) -> None:
        """Test template installation when template file doesn't exist."""
        from llm_orc.cli_modules.commands.config_commands import (
            _install_template_and_restore_auth,
        )

        # Given
        global_config_dir = Path(temp_dir) / "global_config"
        global_config_dir.mkdir(parents=True)

        template_path = Path(temp_dir) / "nonexistent.yaml"
        auth_files: list[tuple[str, bytes]] = []

        # When/Then
        with pytest.raises(click.ClickException, match="Template not found"):
            _install_template_and_restore_auth(
                global_config_dir, template_path, auth_files
            )


class TestResetLocalConfigHelperMethods:
    """Test helper methods from reset_local_config for complexity reduction."""

    @pytest.fixture
    def temp_dir(self) -> Generator[str, None, None]:
        """Create a temporary directory for testing."""
        with tempfile.TemporaryDirectory() as temp_dir:
            yield temp_dir

    def test_create_local_backup_if_requested_with_backup_enabled(
        self, temp_dir: str
    ) -> None:
        """Test local backup creation when backup is requested and config exists."""
        from llm_orc.cli_modules.commands.config_commands import (
            _create_local_backup_if_requested,
        )

        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            (local_config_dir / "config.yaml").write_text("existing: config")

            # When
            _create_local_backup_if_requested(True, local_config_dir)

            # Then
            backup_path = test_dir / ".llm-orc.backup"
            assert backup_path.exists()
            assert (backup_path / "config.yaml").read_text() == "existing: config"
        finally:
            os.chdir(original_cwd)

    def test_create_local_backup_if_requested_overwrite_existing_backup(
        self, temp_dir: str
    ) -> None:
        """Test local backup creation when backup already exists."""
        from llm_orc.cli_modules.commands.config_commands import (
            _create_local_backup_if_requested,
        )

        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            # Create existing local config
            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            (local_config_dir / "config.yaml").write_text("new: config")

            # Create existing backup directory
            backup_path = test_dir / ".llm-orc.backup"
            backup_path.mkdir(parents=True)
            (backup_path / "old_config.yaml").write_text("old: backup")

            # When
            _create_local_backup_if_requested(True, local_config_dir)

            # Then - should overwrite existing backup
            assert backup_path.exists()
            assert (backup_path / "config.yaml").read_text() == "new: config"
            assert not (backup_path / "old_config.yaml").exists()
        finally:
            os.chdir(original_cwd)

    def test_create_local_backup_if_requested_with_backup_disabled(
        self, temp_dir: str
    ) -> None:
        """Test no local backup creation when backup is disabled."""
        from llm_orc.cli_modules.commands.config_commands import (
            _create_local_backup_if_requested,
        )

        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            (local_config_dir / "config.yaml").write_text("existing: config")

            # When
            _create_local_backup_if_requested(False, local_config_dir)

            # Then
            backup_path = test_dir / ".llm-orc.backup"
            assert not backup_path.exists()
        finally:
            os.chdir(original_cwd)

    def test_preserve_ensembles_if_requested_with_ensembles(
        self, temp_dir: str
    ) -> None:
        """Test ensemble preservation when requested and ensembles exist."""
        from llm_orc.cli_modules.commands.config_commands import (
            _preserve_ensembles_if_requested,
        )

        # Given
        local_config_dir = Path(temp_dir) / ".llm-orc"
        ensembles_dir = local_config_dir / "ensembles"
        ensembles_dir.mkdir(parents=True)
        (ensembles_dir / "test-ensemble.yaml").write_text("ensemble: config")
        (ensembles_dir / "another-ensemble.yaml").write_text("another: config")

        # When
        ensembles_backup = _preserve_ensembles_if_requested(True, local_config_dir)

        # Then
        assert len(ensembles_backup) == 2
        assert ensembles_backup["test-ensemble.yaml"] == "ensemble: config"
        assert ensembles_backup["another-ensemble.yaml"] == "another: config"

    def test_preserve_ensembles_if_requested_disabled(self, temp_dir: str) -> None:
        """Test no ensemble preservation when disabled."""
        from llm_orc.cli_modules.commands.config_commands import (
            _preserve_ensembles_if_requested,
        )

        # Given
        local_config_dir = Path(temp_dir) / ".llm-orc"
        ensembles_dir = local_config_dir / "ensembles"
        ensembles_dir.mkdir(parents=True)
        (ensembles_dir / "test-ensemble.yaml").write_text("ensemble: config")

        # When
        ensembles_backup = _preserve_ensembles_if_requested(False, local_config_dir)

        # Then
        assert ensembles_backup == {}

    def test_preserve_ensembles_if_requested_no_ensembles_dir(
        self, temp_dir: str
    ) -> None:
        """Test ensemble preservation when ensembles directory doesn't exist."""
        from llm_orc.cli_modules.commands.config_commands import (
            _preserve_ensembles_if_requested,
        )

        # Given
        local_config_dir = Path(temp_dir) / ".llm-orc"
        local_config_dir.mkdir(parents=True)

        # When
        ensembles_backup = _preserve_ensembles_if_requested(True, local_config_dir)

        # Then
        assert ensembles_backup == {}

    def test_reset_and_initialize_local_config_success(self, temp_dir: str) -> None:
        """Test local config reset and initialization success case."""
        from llm_orc.cli_modules.commands.config_commands import (
            _reset_and_initialize_local_config,
        )

        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)
            (local_config_dir / "old_file.txt").write_text("old content")

            with patch(
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            ) as mock_config_manager_class:
                mock_config_manager = Mock()
                mock_config_manager_class.return_value = mock_config_manager

                # Mock init_local_config to recreate the directory
                def mock_init(project_name: str | None) -> None:
                    local_config_dir.mkdir(parents=True, exist_ok=True)

                mock_config_manager.init_local_config.side_effect = mock_init

                # When
                _reset_and_initialize_local_config(
                    local_config_dir, mock_config_manager, "test-project"
                )

                # Then
                assert local_config_dir.exists()
                assert not (local_config_dir / "old_file.txt").exists()
                mock_config_manager.init_local_config.assert_called_once_with(
                    "test-project"
                )
        finally:
            os.chdir(original_cwd)

    def test_reset_and_initialize_local_config_init_failure(
        self, temp_dir: str
    ) -> None:
        """Test local config reset when initialization fails."""
        from llm_orc.cli_modules.commands.config_commands import (
            _reset_and_initialize_local_config,
        )

        # Given
        original_cwd = Path.cwd()
        test_dir = Path(temp_dir)

        try:
            import os

            os.chdir(test_dir)

            local_config_dir = test_dir / ".llm-orc"
            local_config_dir.mkdir(parents=True)

            with patch(
                "llm_orc.cli_modules.commands.config_commands.ConfigurationManager"
            ) as mock_config_manager_class:
                mock_config_manager = Mock()
                mock_config_manager_class.return_value = mock_config_manager
                mock_config_manager.init_local_config.side_effect = ValueError(
                    "Init failed"
                )

                # When/Then
                with pytest.raises(click.ClickException, match="Init failed"):
                    _reset_and_initialize_local_config(
                        local_config_dir, mock_config_manager, "test-project"
                    )
        finally:
            os.chdir(original_cwd)

    def test_restore_ensembles_and_complete_with_ensembles(self, temp_dir: str) -> None:
        """Test ensemble restoration and completion with ensembles."""
        from llm_orc.cli_modules.commands.config_commands import (
            _restore_ensembles_and_complete,
        )

        # Given
        local_config_dir = Path(temp_dir) / ".llm-orc"
        ensembles_dir = local_config_dir / "ensembles"
        ensembles_dir.mkdir(parents=True)

        ensembles_backup = {
            "test-ensemble.yaml": "ensemble: config",
            "another-ensemble.yaml": "another: config",
        }

        # When
        _restore_ensembles_and_complete(local_config_dir, ensembles_backup, True)

        # Then
        assert (ensembles_dir / "test-ensemble.yaml").read_text() == "ensemble: config"
        assert (
            ensembles_dir / "another-ensemble.yaml"
        ).read_text() == "another: config"

    def test_restore_ensembles_and_complete_without_ensembles(
        self, temp_dir: str
    ) -> None:
        """Test ensemble restoration and completion without ensembles."""
        from llm_orc.cli_modules.commands.config_commands import (
            _restore_ensembles_and_complete,
        )

        # Given
        local_config_dir = Path(temp_dir) / ".llm-orc"
        local_config_dir.mkdir(parents=True)
        ensembles_backup: dict[str, str] = {}

        # When
        _restore_ensembles_and_complete(local_config_dir, ensembles_backup, False)

        # Then - should complete without error


class TestConfigCommandsCLI:
    """Test config commands through CLI interface."""

    @pytest.fixture
    def runner(self) -> CliRunner:
        """Click test runner."""
        return CliRunner()

    def test_config_init_cli_success(self, runner: CliRunner) -> None:
        """Test config init through CLI."""
        with patch("llm_orc.cli.init_local_config") as mock_init:
            result = runner.invoke(
                cli, ["config", "init", "--project-name", "test-project"]
            )

            assert result.exit_code == 0
            mock_init.assert_called_once_with("test-project")

    def test_config_init_cli_error(self, runner: CliRunner) -> None:
        """Test config init CLI error handling."""
        with patch("llm_orc.cli.init_local_config") as mock_init:
            mock_init.side_effect = click.ClickException("Init error")

            result = runner.invoke(cli, ["config", "init"])

            assert result.exit_code != 0
            assert "Init error" in result.output

    def test_config_check_global_cli(self, runner: CliRunner) -> None:
        """Test config check global through CLI."""
        with patch("llm_orc.cli.check_global_config") as mock_check:
            result = runner.invoke(cli, ["config", "check"])

            assert result.exit_code == 0
            mock_check.assert_called_once()

    def test_config_check_local_cli(self, runner: CliRunner) -> None:
        """Test config check local through CLI."""
        with patch("llm_orc.cli.check_local_config") as mock_check:
            result = runner.invoke(cli, ["config", "check-local"])

            assert result.exit_code == 0
            mock_check.assert_called_once()

    def test_config_reset_global_cli(self, runner: CliRunner) -> None:
        """Test config reset global through CLI."""
        with patch("llm_orc.cli.reset_global_config") as mock_reset:
            result = runner.invoke(
                cli,
                ["config", "reset-global", "--backup", "--preserve-auth"],
                input="y\n",
            )

            assert result.exit_code == 0
            mock_reset.assert_called_once_with(True, True)

    def test_config_reset_local_cli(self, runner: CliRunner) -> None:
        """Test config reset local through CLI."""
        with patch("llm_orc.cli.reset_local_config") as mock_reset:
            result = runner.invoke(
                cli,
                [
                    "config",
                    "reset-local",
                    "--backup",
                    "--preserve-ensembles",
                    "--project-name",
                    "test",
                ],
                input="y\n",
            )

            assert result.exit_code == 0
            mock_reset.assert_called_once_with(True, True, "test")
