"""Extra cart query methods not in the CartService protocol.

cart-items returns raw CartItem data without going through CartSummaryDTO.
"""
from __future__ import annotations

from typing import Any

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from shared.models.market import CartItem


class SqlCartItemsService:

    async def cart_items(
        self, session: AsyncSession, *,
        user_id: int | None = None, session_id: str | None = None,
    ) -> list[dict[str, Any]]:
        filters = [CartItem.deleted_at.is_(None)]
        if user_id is not None:
            filters.append(CartItem.user_id == user_id)
        elif session_id is not None:
            filters.append(CartItem.session_id == session_id)
        else:
            return []

        result = await session.execute(select(CartItem).where(*filters))
        return [
            {
                "product_id": item.product_id,
                "product_slug": item.product_slug,
                "quantity": item.quantity,
            }
            for item in result.scalars().all()
        ]
