import pytest
import time
import sys
import os

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))

from src import api_hook_client


@pytest.mark.integration
def test_mma_epic_lifecycle(live_gui) -> None:
 client = api_hook_client.ApiHookClient()
 assert client.wait_for_server(timeout=15)

 # Reset
 client.click("btn_reset")
 time.sleep(2)

 # Set provider and path
 client.set_value("current_provider", "gemini_cli")
 time.sleep(2)
 mock_path = os.path.abspath("tests/mock_gemini_cli.py")
 client.set_value("gcli_path", f'"{sys.executable}" "{mock_path}"')
 time.sleep(2)

 # Set epic and click
 client.set_value("mma_epic_input", "Add timestamps")
 time.sleep(1)
 client.click("btn_mma_plan_epic")

 # Wait and check
 for i in range(30):
  time.sleep(1)
  status = client.get_mma_status()
  proposed = status.get("proposed_tracks", [])
  usage = status.get("mma_tier_usage", {})
  t1 = usage.get("Tier 1", {})

  print(
   f"[{i}] Tier1: in={t1.get('input')}, out={t1.get('output')}, proposed={len(proposed)}",
   flush=True,
  )

  if proposed:
   print(f"SUCCESS: {proposed}", flush=True)
   break

 assert len(proposed) > 0, f"No tracks: {proposed}"
