"""Tests for the API status endpoints.

Verifies /api/briefing/status, /api/memory/status, and /api/swarm/status
return valid JSON with expected keys.
"""


def test_api_briefing_status_returns_ok(client):
    """GET /api/briefing/status returns 200 with expected JSON structure."""
    response = client.get("/api/briefing/status")
    assert response.status_code == 200

    data = response.json()
    assert data["status"] == "ok"
    assert "pending_approvals" in data
    assert isinstance(data["pending_approvals"], int)
    assert "last_generated" in data
    # last_generated can be None or a string
    assert data["last_generated"] is None or isinstance(data["last_generated"], str)


def test_api_memory_status_returns_ok(client):
    """GET /api/memory/status returns 200 with expected JSON structure."""
    response = client.get("/api/memory/status")
    assert response.status_code == 200

    data = response.json()
    assert data["status"] == "ok"
    assert "db_exists" in data
    assert isinstance(data["db_exists"], bool)
    assert "db_size_bytes" in data
    assert isinstance(data["db_size_bytes"], int)
    assert data["db_size_bytes"] >= 0
    assert "indexed_files" in data
    assert isinstance(data["indexed_files"], int)
    assert data["indexed_files"] >= 0


def test_api_swarm_status_returns_ok(client):
    """GET /api/swarm/status returns 200 with expected JSON structure."""
    response = client.get("/api/swarm/status")
    assert response.status_code == 200

    data = response.json()
    assert data["status"] == "ok"
    assert "active_workers" in data
    assert isinstance(data["active_workers"], int)
    assert "pending_tasks" in data
    assert isinstance(data["pending_tasks"], int)
    assert data["pending_tasks"] >= 0
    assert "message" in data
    assert isinstance(data["message"], str)
    assert data["message"] == "Swarm monitoring endpoint"


def test_api_swarm_status_reflects_pending_tasks(client):
    """GET /api/swarm/status reflects pending tasks from task queue."""
    # First create a task
    client.post("/api/tasks", json={"title": "Swarm status test task"})

    # Now check swarm status
    response = client.get("/api/swarm/status")
    assert response.status_code == 200

    data = response.json()
    assert data["pending_tasks"] >= 1


def test_api_briefing_status_pending_approvals_count(client):
    """GET /api/briefing/status returns correct pending approvals count."""
    response = client.get("/api/briefing/status")
    assert response.status_code == 200

    data = response.json()
    assert "pending_approvals" in data
    assert isinstance(data["pending_approvals"], int)
    assert data["pending_approvals"] >= 0
