import time
import pytest
from src import api_hook_client

@pytest.mark.integration
@pytest.mark.live
def test_visual_mma_components(live_gui):
 """
    Refactored visual MMA verification using the live_gui fixture.
    Ensures the MMA dashboard and tickets are correctly rendered.
    """
 # live_gui is a tuple (process, script_name)
 _, gui_script = live_gui
 print(f"Testing visual MMA components on {gui_script}...")

 # 1. Initialize api_hook_client.ApiHookClient
 # The fixture ensures the server is already ready
 client = api_hook_client.ApiHookClient()
 print("ApiHookClient initialized successfully.")

 # 2. Setup MMA data
 track_data = {
  "id": "visual_test_track",
  "title": "Visual Verification Track",
  "description": "A track to verify MMA UI components"
 }
 tickets_data = [
  {"id": "TICKET-001", "target_file": "core.py", "status": "todo", "description": "1", "assigned_to": "Worker"},
  {"id": "TICKET-002", "target_file": "utils.py", "status": "running", "description": "2", "assigned_to": "Worker"},
  {"id": "TICKET-003", "target_file": "tests.py", "status": "complete", "description": "3", "assigned_to": "Worker"},
  {"id": "TICKET-004", "target_file": "api.py", "status": "blocked", "description": "4", "assigned_to": "Worker"},
  {"id": "TICKET-005", "target_file": "gui.py", "status": "paused", "description": "5", "assigned_to": "Worker"},
 ]

 print("\nPushing MMA state update...")
 payload = {
  "status": "running",
  "active_tier": "Tier 3",
  "track": track_data,
  "tickets": tickets_data
 }
 client.push_event("mma_state_update", payload)
 print("  - MMA state update pushed.")

 # Poll for state update
 success = False
 for _ in range(50): # 10 seconds total
  if client.get_value("mma_active_tier") == "Tier 3":
   success = True
   break
  time.sleep(0.2)

 assert success, f"State did not update to Tier 3. Current: {client.get_value('mma_active_tier')}"

 # 3. Trigger HITL modal

 print("Pushing 'mma_step_approval' event to trigger HITL modal...")
 approval_payload = {
  "ticket_id": "TICKET-002",
  "payload": "powershell -Command \"Write-Host 'Hello from Tier 3'\""
 }
 client.push_event("mma_step_approval", approval_payload)
 print("mma_step_approval event pushed successfully.")

 # 4. Assertions
 # We can verify internal state via get_value if hooks are available
 # For now, we verify the push was successful (it would raise if not)
 # and we can check some values that should have changed.
 active_tier = client.get_value("mma_active_tier")
 assert active_tier == "Tier 3"
 
 # Verify ticket count if possible
 # mma_tickets might be a complex object, we'll see if get_value handles it
 tickets = client.get_value("mma_tickets")
 if tickets:
  assert len(tickets) == 5
  assert tickets[1]['id'] == "TICKET-002"
  assert tickets[1]['status'] == "running"

 print("Visual MMA component verification PASSED.")
 
 # Clean up the pending modal to prevent polluting subsequent tests
 print("Cleaning up pending MMA modal...")
 client.post_gui({
  "action": "click",
  "item": "btn_approve_mma_step"
 })
 time.sleep(0.5)
