# products.py
from __future__ import annotations
from typing import List, Optional
from urllib.parse import urlparse

from .state import KNOWN_PRODUCT_SLUGS
from .blacklist.category import is_category_blocked
from . import db_backend as cb

# NEW IMPORT:
from quart import g

async def products(
    list_url: str,
    selected_brands: Optional[List[str]] = None,
    selected_stickers: Optional[List[str]] = None,
    selected_labels: Optional[List[str]] = None,
    page: int = 1,
    search: Optional[str] = None,
    sort: Optional[str] = None,
    liked: Optional[bool] = None,
    user_id: Optional[int] = None,
    market_id: int | None = None,
):
    p = urlparse(list_url)
    parts = [x for x in (p.path or "").split("/") if x]
    top = parts[0] if parts else None
    sub = parts[1] if len(parts) >= 2 else None

    if is_category_blocked(top, sub):
        return [], [], [], [], 1  # <- note: 5 values now, keep shape consistent below
    data = await cb.db_products(
        g.s,
        top,
        sub,
        selected_brands,
        selected_stickers,
        selected_labels,
        page,
        search,
        sort,
        liked=liked,
        user_id = g.user.id if g.user else 0,
        market_id=market_id,
    )
    items = data.get("items", []) or []
    brands = data.get("brands", []) or []
    stickers = data.get("stickers", []) or []
    labels = data.get("labels", []) or []
    total_pages = int(data.get("total_pages", 1) or 1)
    
    # Track known product slugs
    for it in items:
        try:
            slug = it.get("slug")
            if slug:
                KNOWN_PRODUCT_SLUGS.add(slug)
        except Exception:
            pass

    # --- NEW BIT: mark which are liked by this user ---
    

    # Return same shape you were already returning:
    # items, brands, stickers, labels, total_pages
    return items, brands, stickers, labels, total_pages, data.get("liked_count"), data.get("search_count")


async def products_nocounts(
    session,
    list_url: str,
    selected_brands: Optional[List[str]] = None,
    selected_stickers: Optional[List[str]] = None,
    selected_labels: Optional[List[str]] = None,
    page: int = 1,
    search: Optional[str] = None,
    sort: Optional[str] = None,
    liked: Optional[bool] = None,
    user_id: Optional[int] = None,
    market_id: int | None = None,
):
    p = urlparse(list_url)
    parts = [x for x in (p.path or "").split("/") if x]
    top = parts[0] if parts else None
    sub = parts[1] if len(parts) >= 2 else None

    if is_category_blocked(top, sub):
        return [], [], [], [], 1  # <- note: 5 values now, keep shape consistent below
    data = await cb.db_products_nocounts(
        session,
        top,
        sub,
        selected_brands,
        selected_stickers,
        selected_labels,
        page,
        search,
        sort,
        liked=liked,
        user_id = g.user.id if g.user else 0,
        market_id=market_id,
    )
    items = data.get("items", []) or []
    total_pages = int(data.get("total_pages", 1) or 1)
    
    # Track known product slugs
    for it in items:
        try:
            slug = it.get("slug")
            if slug:
                KNOWN_PRODUCT_SLUGS.add(slug)
        except Exception:
            pass

    
    # Return same shape you were already returning:
    # items, brands, stickers, labels, total_pages
    return items, total_pages
