"""
smoke_test_live.py — Phase 1 live test. Requires a real API key in .env.

Tests the full agent loop end-to-end with EchoTool:
  1. Agent calls EchoTool in response to a user message
  2. Receives tool result and produces a final text response
  3. All events are logged

Run: python smoke_test_live.py
"""
from __future__ import annotations

import asyncio
import sys
import os

sys.path.insert(0, os.path.dirname(__file__))


async def run():
    print("=" * 60)
    print("aide — Phase 1 Live Agent Test")
    print("=" * 60)

    from server.database import init_db
    init_db()

    from server.agent.tool_registry import ToolRegistry
    from server.tools.mock import EchoTool, ConfirmTool
    from server.agent.agent import Agent, run_and_collect, DoneEvent, ErrorEvent, ToolStartEvent, ToolDoneEvent

    registry = ToolRegistry()
    registry.register(EchoTool())
    registry.register(ConfirmTool())

    agent = Agent(registry=registry)

    print("\n[Test 1] Echo tool call")
    print("-" * 40)
    message = 'Please use the echo tool to echo back the phrase "Phase 1 works!"'

    text, calls, usage, events = await run_and_collect(
        agent=agent,
        message=message,
        session_id="live-test-1",
    )

    print(f"Events received: {len(events)}")
    for event in events:
        if isinstance(event, ToolStartEvent):
            print(f"  → Tool call: {event.tool_name}({event.arguments})")
        elif isinstance(event, ToolDoneEvent):
            print(f"  ← Tool done: success={event.success}, result={event.result_summary!r}")
        elif isinstance(event, ErrorEvent):
            print(f"  ✗ Error: {event.message}")

    print(f"\nFinal text:\n{text}")
    print(f"Tool calls made: {calls}")
    print(f"Tokens: {usage.input_tokens} in / {usage.output_tokens} out")

    if calls == 0:
        print("\nWARNING: No tool calls were made. The model may not have used the tool.")
    elif not isinstance(events[-1], ErrorEvent):
        print("\n✓ Live agent test passed")
    else:
        print("\n✗ Live agent test failed — see error above")
        sys.exit(1)

    print("\n[Test 2] Kill switch")
    print("-" * 40)
    from server.database import credential_store
    credential_store.set("system:paused", "1")
    _, _, _, events = await run_and_collect(agent=agent, message="hello")
    assert any(isinstance(e, ErrorEvent) for e in events), "Kill switch did not block agent"
    credential_store.delete("system:paused")
    print("✓ Kill switch blocks agent when paused")

    print("\n" + "=" * 60)
    print("Live tests complete ✓")
    print("=" * 60)


if __name__ == "__main__":
    asyncio.run(run())
