import pytest
import sys
import os

# Ensure project root is in path for imports
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from api_hook_client import ApiHookClient

def test_api_client_has_extensions():
    client = ApiHookClient()
    # These should fail initially as they are not implemented
    assert hasattr(client, 'select_tab')
    assert hasattr(client, 'select_list_item')

def test_select_tab_integration(live_gui):
    client = ApiHookClient()
    # We'll need to make sure the tags exist in gui_legacy.py
    # For now, this is a placeholder for the integration test
    response = client.select_tab("operations_tabs", "tab_tool")
    assert response == {'status': 'queued'}

def test_select_list_item_integration(live_gui):
    client = ApiHookClient()
    # Assuming 'Default' discussion exists or we can just test that it queues
    response = client.select_list_item("disc_listbox", "Default")
    assert response == {'status': 'queued'}

def test_get_indicator_state_integration(live_gui):
    client = ApiHookClient()
    # thinking_indicator is usually hidden unless AI is running
    response = client.get_indicator_state("thinking_indicator")
    assert 'shown' in response
    assert response['tag'] == "thinking_indicator"

def test_app_processes_new_actions():
    import gui_legacy
    from unittest.mock import MagicMock, patch
    import dearpygui.dearpygui as dpg

    dpg.create_context()
    try:
        with patch('gui_legacy.load_config', return_value={}), \
             patch('gui_legacy.PerformanceMonitor'), \
             patch('gui_legacy.shell_runner'), \
             patch('gui_legacy.project_manager'), \
             patch.object(gui_legacy.App, '_load_active_project'):
            app = gui_legacy.App()
            
            with patch('dearpygui.dearpygui.set_value') as mock_set_value, \
                 patch('dearpygui.dearpygui.does_item_exist', return_value=True), \
                 patch('dearpygui.dearpygui.get_item_callback') as mock_get_cb:
                
                # Test select_tab
                app._pending_gui_tasks.append({
                    "action": "select_tab",
                    "tab_bar": "some_tab_bar",
                    "tab": "some_tab"
                })
                app._process_pending_gui_tasks()
                mock_set_value.assert_any_call("some_tab_bar", "some_tab")
                
                # Test select_list_item
                mock_cb = MagicMock()
                mock_get_cb.return_value = mock_cb
                app._pending_gui_tasks.append({
                    "action": "select_list_item",
                    "listbox": "some_listbox",
                    "item_value": "some_value"
                })
                app._process_pending_gui_tasks()
                mock_set_value.assert_any_call("some_listbox", "some_value")
                mock_cb.assert_called_with("some_listbox", "some_value")
    finally:
        dpg.destroy_context()
