"""Tests for theme picker and /themes command."""

from __future__ import annotations

from pathlib import Path

import pytest

from clients.terminal.tui.tui_app import NaviCodeTui


@pytest.fixture(autouse=True)
def tmp_state_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
    """Override the state dir so tests never touch ~/.navi_code."""
    from clients.terminal import config

    original = config.settings.state_dir
    config.settings.state_dir = tmp_path
    import clients.terminal.tui.settings as settings_module

    settings_module._tui_settings = None
    yield tmp_path
    config.settings.state_dir = original
    settings_module._tui_settings = None


@pytest.mark.anyio
async def test_themes_command_opens_picker() -> None:
    async with NaviCodeTui(new_session=True).run_test() as pilot:
        await pilot.pause()
        input_box = pilot.app.query_one("InputBox")
        input_box._input.value = "/themes"
        await pilot.press("enter")
        await pilot.pause()
        assert pilot.app.screen.__class__.__name__ == "ThemePickerScreen"


@pytest.mark.anyio
async def test_themes_command_switches_theme() -> None:
    async with NaviCodeTui(new_session=True).run_test() as pilot:
        await pilot.pause()
        input_box = pilot.app.query_one("InputBox")
        input_box._input.value = "/themes"
        await pilot.press("enter")
        await pilot.pause()
        assert pilot.app.screen.__class__.__name__ == "ThemePickerScreen"
        await pilot.press("down")
        await pilot.press("enter")
        await pilot.pause()
        assert pilot.app._theme_name == "gnexus-light"


@pytest.mark.anyio
async def test_mouse_command_toggles_setting() -> None:
    async with NaviCodeTui(new_session=True).run_test() as pilot:
        await pilot.pause()
        from clients.terminal.tui.settings import get_tui_settings

        tui_settings = get_tui_settings()
        original = tui_settings.mouse
        input_box = pilot.app.query_one("InputBox")
        input_box._input.value = "/mouse"
        await pilot.press("enter")
        await pilot.pause()
        assert tui_settings.mouse is not original
