from django.shortcuts import render, get_object_or_404
from .models import Category, Product

def home(request):
    """Landing page with featured categories and products."""
    categories = Category.objects.all()[:4]
    featured_products = Product.objects.filter(is_active=True)[:6]
    context = {
        "categories": categories,
        "featured_products": featured_products,
        "project_name": "GiftShop Polska",
    }
    return render(request, "core/index.html", context)

def catalog(request):
    """Product catalog with category filtering."""
    category_slug = request.GET.get('category')
    categories = Category.objects.all()
    products = Product.objects.filter(is_active=True)
    
    selected_category = None
    if category_slug:
        selected_category = get_object_or_404(Category, slug=category_slug)
        products = products.filter(category=selected_category)
        
    context = {
        "categories": categories,
        "products": products,
        "selected_category": selected_category,
    }
    return render(request, "core/catalog.html", context)

def product_detail(request, slug):
    """Individual product detail page."""
    product = get_object_or_404(Product, slug=slug, is_active=True)
    context = {
        "product": product,
    }
    return render(request, "core/product_detail.html", context)

def constructor(request):
    """Gift box constructor page."""
    return render(request, "core/constructor.html", {"title": "Box Constructor"})

def cart(request):
    """Shopping cart page."""
    return render(request, "core/cart.html", {"title": "Your Cart"})

def favorites(request):
    """Favorites/Wishlist page."""
    return render(request, "core/favorites.html", {"title": "Your Favorites"})

def blog(request):
    """Blog index page."""
    return render(request, "core/blog.html", {"title": "Gift Shop Blog"})