import os
import sys
import pytest
import requests
import json
from unittest.mock import patch

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

from api_hook_client import ApiHookClient
import gui_legacy

def test_hooks_enabled_via_cli():
    with patch.object(sys, 'argv', ['gui_legacy.py', '--enable-test-hooks']):
        app = gui_legacy.App()
        assert app.test_hooks_enabled is True

def test_hooks_disabled_by_default():
    with patch.object(sys, 'argv', ['gui_legacy.py']):
        if 'SLOP_TEST_HOOKS' in os.environ:
            del os.environ['SLOP_TEST_HOOKS']
        app = gui_legacy.App()
        assert getattr(app, 'test_hooks_enabled', False) is False

def test_live_hook_server_responses(live_gui):
    """
    Verifies the live hook server (started via fixture) responds correctly to all major endpoints.
    """
    client = ApiHookClient()
    
    # Test /status
    status = client.get_status()
    assert status == {'status': 'ok'}
    
    # Test /api/project
    project = client.get_project()
    assert 'project' in project
    
    # Test /api/session
    session = client.get_session()
    assert 'session' in session
    
    # Test /api/performance
    perf = client.get_performance()
    assert 'performance' in perf
    
    # Test POST /api/gui
    gui_data = {"action": "test_action", "value": 42}
    resp = client.post_gui(gui_data)
    assert resp == {'status': 'queued'}
