"""Extra market query methods for raw-SQLAlchemy data lookups.

products-by-ids and marketplaces-by-ids use direct selects rather
than the MarketService protocol methods.
"""
from __future__ import annotations

from typing import Any

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


class SqlMarketDataService:

    async def products_by_ids(
        self, session: AsyncSession, *, ids: list[int],
    ) -> list[dict[str, Any]]:
        if not ids:
            return []
        from shared.models.market import Product
        rows = (await session.execute(
            select(Product).where(Product.id.in_(ids))
        )).scalars().all()
        return [
            {
                "id": p.id,
                "title": p.title,
                "slug": p.slug,
                "image": p.image,
                "regular_price": str(p.regular_price) if p.regular_price is not None else None,
                "special_price": str(p.special_price) if p.special_price is not None else None,
            }
            for p in rows
        ]

    async def marketplaces_by_ids(
        self, session: AsyncSession, *, ids: list[int],
    ) -> list[dict[str, Any]]:
        if not ids:
            return []
        from shared.models.market_place import MarketPlace
        rows = (await session.execute(
            select(MarketPlace).where(MarketPlace.id.in_(ids))
        )).scalars().all()
        return [
            {
                "id": m.id,
                "name": m.name,
                "slug": m.slug,
                "container_type": m.container_type,
                "container_id": m.container_id,
            }
            for m in rows
        ]
