from django.shortcuts import render, get_object_or_404
from .models import ProductBrand, ProductCategory, Product
from django.http import JsonResponse
# List Miavit products
def miavit_products(request):
    brand = get_object_or_404(ProductBrand, slug="miavit")
    categories = ProductCategory.objects.filter(brand=brand)
    return render(request, "products/product_list.html", {"brand": brand, "categories": categories})

# List Koudijs products
def koudijs_products(request):
    brand = get_object_or_404(ProductBrand, slug="koudijs")
    categories = ProductCategory.objects.filter(brand=brand)
    return render(request, "products/product_list.html", {"brand": brand, "categories": categories})

# Product list for category
def product_list(request, brand_slug):
    brand = get_object_or_404(ProductBrand,slug=brand_slug)
    categories = ProductCategory.objects.filter(products__brand=brand).distinct()  # ✅ FIXED
    products = Product.objects.filter(brand=brand)

    context = {
        "brand": brand,
        "categories": categories,
        "products": products,
    }
    return render(request, "products/product_list.html", context)

# Product detail page
def product_detail(request, brand_slug, category_slug, product_slug):
    # Get the brand object
    brand = ProductBrand.objects.get(slug=brand_slug)

    # Get the category object (filtering only by slug)
    category = get_object_or_404(ProductCategory, slug=category_slug)

    # Get the product within the given brand and category
    product = get_object_or_404(Product, brand=brand, category=category, slug=product_slug)

    # **FIX**: Define `related_products` (e.g., similar products from the same category)
    related_products = Product.objects.filter(category=category).exclude(id=product.id)[:4]

    context = {
        'brand': brand,
        'category': category,
        'product': product,
        'related_products': related_products
    }
    return render(request, "products/product_detail.html", context)


def energy_suppliments(request):
    products = Product.objects.filter(category__slug="energy_suppliments")
    return render(request, "products/energy_suppliments.html", {"products": products})

def Toxin_management(request):
    products = Product.objects.filter(category__slug="Toxin_management")
    return render(request, "products/Toxin_management.html", {"products": products})

def liver_support(request):
    products = Product.objects.filter(category__slug="liver_support")
    return render(request, "products/liver_support.html", {"products": products})

def feed_protection(request):
    products = Product.objects.filter(category__slug="feed_protection")
    return render(request, "products/feed_protection.html", {"products": products})

def growth_support(request):
    products = Product.objects.filter(category__slug="growth_support")
    return render(request, "products/growth_support.html", {"products": products})

def premixes(request):
    products = Product.objects.filter(category__slug="premixes")
    return render(request, "products/premixes.html", {"products": products})

def raw_materials(request):
    products = Product.objects.filter(category__slug="raw_materials")
    return render(request, "products/raw_materials.html", {"products": products})

def get_products_by_brands(request):
    """Fetch products based on selected brands."""
    brand_ids = request.GET.getlist('brands[]')
    products = Product.objects.filter(brand__id__in=brand_ids).values('id', 'name')
    return JsonResponse({'products': list(products)})

def product_overview(request):
    categories = ProductCategory.objects.all()
    return render(request, "products/overview.html", {"categories": categories})

