from django.shortcuts import render, get_object_or_404
from .models import Product, Variation
from django.db.models import Q

def product_list(request):
    query = request.GET.get('q')
    products = Product.objects.all()
    
    if query:
        products = products.filter(
            Q(name__icontains=query) | Q(sku__icontains=query)
        )
    
    return render(request, 'core/index.html', {
        'products': products,
        'query': query
    })

def product_detail(request, pk):
    product = get_object_or_404(Product, pk=pk)
    variations = product.variations.all()
    
    return render(request, 'core/product_detail.html', {
        'product': product,
        'variations': variations
    })