from django.urls import reverse

from oscar.core.loading import get_model

from .constants import COMPARISION_SESSION_KEY

Product = get_model("catalogue", "Product")


def in_comparison(request):
    product_ids = request.session.get(COMPARISION_SESSION_KEY, [])
    products_for_comparison = Product.objects.filter(id__in=product_ids)
    products_for_comparison_url = (
        (reverse("compare:products", kwargs={"products": product_ids}))
        if product_ids
        else "#"
    )
    return {
        "products_for_comparison": products_for_comparison,
        "products_for_comparison_url": products_for_comparison_url,
    }
