from types import SimpleNamespace

from sqlalchemy import select

from shared.models.market import CartItem
from .identity import current_cart_identity


def _attach_product_namespace(ci: CartItem) -> None:
    """Build a SimpleNamespace 'product' from denormalized columns for template compat."""
    ci.product = SimpleNamespace(
        id=ci.product_id,
        title=ci.product_title,
        slug=ci.product_slug,
        image=ci.product_image,
        brand=ci.product_brand,
        regular_price=ci.product_regular_price,
        special_price=ci.product_special_price,
        regular_price_currency=ci.product_price_currency,
    )


def _attach_market_place_namespace(ci: CartItem) -> None:
    """Build a SimpleNamespace 'market_place' from denormalized columns."""
    if ci.market_place_id:
        ci.market_place = SimpleNamespace(
            id=ci.market_place_id,
            name=ci.market_place_name,
            container_id=ci.market_place_container_id,
        )
    else:
        ci.market_place = None


async def get_cart(session):
    ident = current_cart_identity()

    filters = [CartItem.deleted_at.is_(None)]
    if ident["user_id"] is not None:
        filters.append(CartItem.user_id == ident["user_id"])
    else:
        filters.append(CartItem.session_id == ident["session_id"])

    result = await session.execute(
        select(CartItem)
        .where(*filters)
        .order_by(CartItem.created_at.desc())
    )
    items = list(result.scalars().all())

    for ci in items:
        _attach_product_namespace(ci)
        _attach_market_place_namespace(ci)

    return items
