import pytest
from unittest.mock import patch
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 src.api_hook_client import ApiHookClient

def test_get_status_success() -> None:
 """Test that get_status successfully retrieves the server status"""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {"status": "ok", "provider": "gemini"}
  status = client.get_status()
  assert status["status"] == "ok"
  mock_make.assert_any_call('GET', '/status')

def test_get_project_success() -> None:
 """Test successful retrieval of project data from the /api/project endpoint"""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {"project": {"name": "test"}}
  project = client.get_project()
  assert project["project"]["name"] == "test"
  mock_make.assert_any_call('GET', '/api/project')

def test_get_session_success() -> None:
 """Test successful retrieval of session history from the /api/session endpoint"""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {"session": {"entries": []}}
  session = client.get_session()
  assert "session" in session
  mock_make.assert_any_call('GET', '/api/session')

def test_post_gui_success() -> None:
 """Test that post_gui correctly sends a POST request to the /api/gui endpoint"""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {"status": "queued"}
  payload = {"action": "click", "item": "btn_reset"}
  res = client.post_gui(payload)
  assert res["status"] == "queued"
  mock_make.assert_any_call('POST', '/api/gui', data=payload)

def test_get_performance_success() -> None:
 """Test retrieval of performance metrics from the /api/gui/diagnostics endpoint"""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {"fps": 60.0}
  metrics = client.get_gui_diagnostics()
  assert metrics["fps"] == 60.0
  mock_make.assert_any_call('GET', '/api/gui/diagnostics')

def test_unsupported_method_error() -> None:
 """Test that ApiHookClient handles unsupported HTTP methods gracefully"""
 client = ApiHookClient()
 # Testing the internal _make_request with an invalid method
 with pytest.raises(ValueError, match="Unsupported HTTP method"):
  client._make_request('INVALID', '/status')

def test_get_text_value() -> None:
 """Test retrieval of string representation using get_text_value."""
 client = ApiHookClient()
 # Mock get_gui_state which is called by get_value
 with patch.object(client, 'get_gui_state') as mock_state:
  mock_state.return_value = {"some_label": "Hello World"}
  val = client.get_text_value("some_label")
  assert val == "Hello World"

def test_get_node_status() -> None:
 """Test retrieval of DAG node status using get_node_status."""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {
   "id": "T1",
   "status": "todo",
   "assigned_to": "worker1"
  }
  status = client.get_node_status("T1")
  assert status["status"] == "todo"
  mock_make.assert_any_call('GET', '/api/mma/node/T1')

def test_drag_success() -> None:
 """Test that drag correctly sends a POST request to the /api/gui endpoint."""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {"status": "queued"}
  res = client.drag("src_id", "dst_id")
  assert res["status"] == "queued"
  mock_make.assert_any_call('POST', '/api/gui', data={
   "action": "drag",
   "src_item": "src_id",
   "dst_item": "dst_id"
  })

def test_right_click_success() -> None:
 """Test that right_click correctly sends a POST request to the /api/gui endpoint."""
 client = ApiHookClient()
 with patch.object(client, '_make_request') as mock_make:
  mock_make.return_value = {"status": "queued"}
  res = client.right_click("item_id")
  assert res["status"] == "queued"
  mock_make.assert_any_call('POST', '/api/gui', data={
   "action": "right_click",
   "item": "item_id"
  })