from django.contrib import admin
from .models import (
    Product,
    ProductFeature,
    ProductDescription,
    ProductImage,
    Category,
)


class ProductImageInline(admin.TabularInline):
    model = ProductImage
    extra = 1
    ordering = ["order"]


class ProductFeatureInline(admin.TabularInline):
    model = ProductFeature
    extra = 1


class ProductDescriptionInline(admin.StackedInline):
    model = ProductDescription
    max_num = 1
    can_delete = False


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "family")
    search_fields = ("name", "slug", "family")
    prepopulated_fields = {"slug": ("name",)}
    inlines = [ProductDescriptionInline, ProductFeatureInline, ProductImageInline]


@admin.register(ProductFeature)
class ProductFeatureAdmin(admin.ModelAdmin):
    list_display = ("product", "name")
    search_fields = ("product__name", "name")


@admin.register(ProductDescription)
class ProductDescriptionAdmin(admin.ModelAdmin):
    list_display = ("product", "housing", "diffuser")
    search_fields = ("product__name", "housing", "diffuser")


@admin.register(ProductImage)
class ProductImageAdmin(admin.ModelAdmin):
    list_display = ("product", "is_drawing", "angle", "order")
    list_filter = ("is_drawing",)
    ordering = ("product", "order")


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ("category_name", "icon")
    search_fields = ("category_name",)
