"""
Tests API produits en lecture seule.
"""

from datetime import datetime

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from pricewatch.app.api.main import list_products
from pricewatch.app.db.models import Base, Product


def test_list_products():
    """Liste des produits."""
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    product = Product(
        source="amazon",
        reference="REF1",
        url="https://example.com",
        title="Produit",
        category="Test",
        currency="EUR",
        first_seen_at=datetime(2026, 1, 14, 16, 0, 0),
        last_updated_at=datetime(2026, 1, 14, 16, 0, 0),
    )
    session.add(product)
    session.commit()

    data = list_products(session=session, limit=50, offset=0)
    assert len(data) == 1
    assert data[0].reference == "REF1"
    session.close()
    engine.dispose()
