"""IT-DASH-001 / IT-PUSH-002 — dashboard stats and sync logs pagination."""

from __future__ import annotations

from unittest.mock import AsyncMock, MagicMock

import pytest

from server.api.servers import get_all_sync_logs, server_stats

pytestmark = pytest.mark.integration


@pytest.mark.asyncio
async def test_server_stats_returns_counts(db_session, test_admin, test_server, monkeypatch):
    async def fake_scan(cursor, match=None, count=200):
        return 0, []

    mock_redis = AsyncMock()
    mock_redis.scan = fake_scan
    monkeypatch.setattr("server.api.servers.get_redis", lambda: mock_redis)

    body = await server_stats(test_admin["admin"], db_session)

    assert "total" in body
    assert body["total"] >= 1
    assert "online" in body
    assert "unset_path" in body
    assert isinstance(body["unset_path"], int)
    assert "online_list" in body
    assert "offline_list" in body
    assert body["online"] + body["offline"] <= body["total"]
    assert body["online_list"] + body["offline_list"] <= body["total"]
    assert "categories" in body


@pytest.mark.asyncio
async def test_sync_logs_paginated(db_session, test_admin):
    body = await get_all_sync_logs(
        server_id=None,
        status=None,
        sync_mode=None,
        trigger_type=None,
        page=1,
        per_page=20,
        admin=test_admin["admin"],
        db=db_session,
    )
    assert "items" in body
    assert "total" in body
    assert isinstance(body["items"], list)
