import logging

from fastapi.testclient import TestClient


def test_memory_system_server_exposes_routes():
    from memory_system_api.server import app

    paths = {route.path for route in app.routes}
    assert "/memory-system/users" in paths
    assert "/memory-system/messages" in paths
    assert "/memory-system/sessions/{session_id}/context" in paths
    context_methods = {
        method
        for route in app.routes
        if getattr(route, "path", "") == "/memory-system/sessions/{session_id}/context"
        for method in getattr(route, "methods", set())
    }
    assert {"GET", "POST"} <= context_methods
    assert "/memory-system/search" in paths
    assert "/memory-system/resources" in paths
    assert "/memory-system/memories" in paths
    assert "/memory-system/memories/content" in paths
    assert "/memory-system/users/{user_id}/profile" in paths
    task_methods = {
        method
        for route in app.routes
        if getattr(route, "path", "") == "/memory-system/openviking/tasks/{task_id}"
        for method in getattr(route, "methods", set())
    }
    profile_methods = {
        method
        for route in app.routes
        if getattr(route, "path", "") == "/memory-system/users/{user_id}/profile"
        for method in getattr(route, "methods", set())
    }
    assert {"GET", "POST"} <= task_methods
    assert {"GET", "POST"} <= profile_methods
    resource_methods = {
        method
        for route in app.routes
        if getattr(route, "path", "") == "/memory-system/resources"
        for method in getattr(route, "methods", set())
    }
    assert {"DELETE", "POST"} <= resource_methods
    memory_methods = {
        method
        for route in app.routes
        if getattr(route, "path", "") == "/memory-system/memories"
        for method in getattr(route, "methods", set())
    }
    memory_content_methods = {
        method
        for route in app.routes
        if getattr(route, "path", "") == "/memory-system/memories/content"
        for method in getattr(route, "methods", set())
    }
    assert {"DELETE", "GET", "POST"} <= memory_methods
    assert {"GET"} <= memory_content_methods


def test_memory_system_messages_does_not_require_account_key_header():
    from memory_system_api.server import app

    route = next(route for route in app.routes if getattr(route, "path", "") == "/memory-system/messages")

    assert all(getattr(dependency.call, "__name__", "") != "account_key_header" for dependency in route.dependant.dependencies)


def test_memory_system_logs_request_and_response_bodies(caplog):
    from memory_system_api.api import get_service
    from memory_system_api.server import app

    class FakeService:
        async def create_user(self, user_id: str):
            return {"status": "success", "account": {"user_id": user_id}}

    app.dependency_overrides[get_service] = lambda: FakeService()

    try:
        with caplog.at_level(logging.INFO, logger="memory_system_api.requests"):
            response = TestClient(app).post("/memory-system/users", json={"user_id": "userA"})
    finally:
        app.dependency_overrides.clear()

    assert response.status_code == 200
    assert response.json() == {"status": "success", "account": {"user_id": "userA"}}
    assert any("request POST /memory-system/users body={\"user_id\":\"userA\"}" in record.message for record in caplog.records)
    assert any(
        "response POST /memory-system/users status=200 body={\"status\":\"success\",\"account\":{\"user_id\":\"userA\"}}"
        in record.message
        for record in caplog.records
    )
