"""Workshop world state API and WebSocket relay.

Serves Timmy's current presence state to the Workshop 3D renderer.
The primary consumer is the browser on first load — before any
WebSocket events arrive, the client needs a full state snapshot.

The ``/ws/world`` endpoint streams ``timmy_state`` messages whenever
the heartbeat detects a state change.  It also accepts ``visitor_message``
frames from the 3D client and responds with ``timmy_speech`` barks.

Source of truth: ``~/.timmy/presence.json`` written by
:class:`~timmy.workshop_state.WorkshopHeartbeat`.
Falls back to a live ``get_state_dict()`` call if the file is stale
or missing.
"""

from fastapi import APIRouter

# Import submodule routers
from .bark import matrix_router as _bark_matrix_router
from .matrix import matrix_router as _matrix_matrix_router
from .state import router as _state_router
from .websocket import router as _ws_router

# ---------------------------------------------------------------------------
# Combine sub-routers into the two top-level routers that app.py expects
# ---------------------------------------------------------------------------

router = APIRouter(prefix="/api/world", tags=["world"])

# Include state routes (GET /state)
for route in _state_router.routes:
    router.routes.append(route)

# Include websocket routes (WS /ws)
for route in _ws_router.routes:
    router.routes.append(route)

# Combine matrix sub-routers
matrix_router = APIRouter(prefix="/api/matrix", tags=["matrix"])

for route in _bark_matrix_router.routes:
    matrix_router.routes.append(route)

for route in _matrix_matrix_router.routes:
    matrix_router.routes.append(route)

# ---------------------------------------------------------------------------
# Re-export public API for backward compatibility
# ---------------------------------------------------------------------------

# Used by src/dashboard/app.py
# Used by tests
from .bark import (  # noqa: E402, F401
    _BARK_RATE_LIMIT_SECONDS,
    _GROUND_TTL,
    _MAX_EXCHANGES,
    BarkRequest,
    _bark_and_broadcast,
    _bark_last_request,
    _conversation,
    _generate_bark,
    _handle_client_message,
    _log_bark_failure,
    _refresh_ground,
    post_matrix_bark,
    reset_conversation_ground,
)
from .commitments import (  # noqa: E402, F401
    _COMMITMENT_PATTERNS,
    _MAX_COMMITMENTS,
    _REMIND_AFTER,
    _build_commitment_context,
    _commitments,
    _extract_commitments,
    _record_commitments,
    _tick_commitments,
    close_commitment,
    get_commitments,
    reset_commitments,
)
from .matrix import (  # noqa: E402, F401
    _DEFAULT_MATRIX_CONFIG,
    _build_matrix_agents_response,
    _build_matrix_health_response,
    _build_matrix_memory_response,
    _build_matrix_thoughts_response,
    _check_capability_bark,
    _check_capability_familiar,
    _check_capability_lightning,
    _check_capability_memory,
    _check_capability_thinking,
    _load_matrix_config,
    _memory_search_last_request,
    get_matrix_agents,
    get_matrix_config,
    get_matrix_health,
    get_matrix_memory_search,
    get_matrix_thoughts,
)
from .state import (  # noqa: E402, F401
    _STALE_THRESHOLD,
    _build_world_state,
    _get_current_state,
    _read_presence_file,
    get_world_state,
)
from .utils import (  # noqa: E402, F401
    _compute_circular_positions,
    _get_agent_color,
    _get_agent_shape,
    _get_client_ip,
)

# Used by src/infrastructure/presence.py
from .websocket import (  # noqa: E402, F401
    _authenticate_ws,
    _broadcast,
    _heartbeat,
    _ws_clients,  # noqa: E402, F401
    broadcast_world_state,  # noqa: E402, F401
    world_ws,
)
