"""
Tests API logs backend.
"""

from pricewatch.app.api.main import BACKEND_LOGS, list_backend_logs, preview_scrape
from pricewatch.app.api.schemas import ScrapePreviewRequest
from pricewatch.app.core.schema import DebugInfo, DebugStatus, FetchMethod, ProductSnapshot


def test_backend_logs_capture_preview(monkeypatch):
    BACKEND_LOGS.clear()

    snapshot = ProductSnapshot(
        source="amazon",
        url="https://example.com",
        title="Produit",
        price=9.99,
        currency="EUR",
        debug=DebugInfo(method=FetchMethod.HTTP, status=DebugStatus.SUCCESS),
    )

    def fake_scrape(url, use_playwright=None, save_db=False):
        return {"success": True, "snapshot": snapshot, "error": None}

    monkeypatch.setattr("pricewatch.app.api.main.scrape_product", fake_scrape)

    preview_scrape(ScrapePreviewRequest(url="https://example.com"))
    logs = list_backend_logs()
    assert logs
    assert logs[-1].message.startswith("Preview scraping")
