from django.contrib import admin
from .models import Product, Color, Size, Order, OrderItem, GuestUser
from django.utils.html import format_html

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ("title", "ordering_value", "price", "cut")

@admin.register(Color)
class ColorAdmin(admin.ModelAdmin):
    list_display = ("color_preview", "name", "ordering", "colorHex", "secondary_hex_color")
    list_editable = ("ordering",)
    ordering = ["ordering"]
    search_fields = ["name"]
    list_per_page = 20

    def color_preview(self, obj):
        if obj.secondary_hex_color:
            return format_html(
                '<div style="background-image: linear-gradient(to right, {} 50%, {} 50%); '
                'width: 60px; height: 30px; border-radius: 15px; border: 1px solid #ddd;"></div>',
                obj.colorHex, obj.secondary_hex_color
            )
        return format_html(
            '<div style="background-color: {}; width: 60px; height: 30px; '
            'border-radius: 15px; border: 1px solid #ddd;"></div>',
            obj.colorHex
        )

@admin.register(Size)
class SizeAdmin(admin.ModelAdmin):
    list_display = ("name",)

class OrderItemInline(admin.TabularInline):
    model = OrderItem
    extra = 0
    readonly_fields = ('product', 'quantity', 'color', 'size', 'price')

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ('id', 'date_ordered', 'status', 'total_price')
    inlines = [OrderItemInline]

class GuestUserOrderInline(admin.TabularInline):
    model = Order
    extra = 0
    readonly_fields = ('date_ordered', 'total_price')
    can_delete = False
    show_change_link = True
    exclude = ('user',)  # Exclude the user field from the inline display

@admin.register(GuestUser)
class GuestUserAdmin(admin.ModelAdmin):
    list_display = ('email', 'phone')
    inlines = [GuestUserOrderInline]
