"""IT-PUSH-001 — POST /api/sync/files returns accepted immediately."""

from __future__ import annotations

from unittest.mock import MagicMock

import pytest

from server.api.schemas import SyncFiles
from server.api.sync_v2 import sync_files

pytestmark = pytest.mark.integration


@pytest.mark.asyncio
async def test_sync_files_returns_accepted_without_blocking(
    db_session,
    test_admin,
    test_server,
    monkeypatch,
):
    scheduled: list[dict] = []

    def fake_schedule(**kwargs):
        scheduled.append(kwargs)

    monkeypatch.setattr(
        "server.background.sync_push_runner.schedule_sync_files",
        fake_schedule,
    )
    monkeypatch.setattr(
        "server.application.services.sync_engine_v2._nexus_source_path",
        lambda _path: "/tmp/nexus-test-src",
    )

    request = MagicMock()
    request.state.db = db_session
    request.client = MagicMock()
    request.client.host = "127.0.0.1"

    batch_id = "aabbccddeeff"
    payload = SyncFiles(
        server_ids=[test_server.id],
        source_path="/tmp/nexus-test-src",
        sync_mode="incremental",
        batch_id=batch_id,
    )

    body = await sync_files(payload, request, test_admin["admin"])

    assert body["accepted"] is True
    assert body["batch_id"] == batch_id
    assert body["total"] == 1
    assert body["completed"] == 0
    assert len(scheduled) == 1
    assert scheduled[0]["batch_id"] == batch_id
    assert scheduled[0]["server_ids"] == [test_server.id]
