from django.contrib import admin
from sales.models import Sale, SaleItem


class SaleItemInline(admin.TabularInline):
    model = SaleItem
    extra = 0
    readonly_fields = ('id',)


@admin.register(Sale)
class SaleAdmin(admin.ModelAdmin):
    list_display = ('title', 'business', 'status', 'pricing_model', 'start_at', 'end_at', 'view_count')
    list_filter = ('status', 'pricing_model', 'start_at')
    search_fields = ('title', 'slug', 'business__name')
    readonly_fields = ('id', 'view_count', 'created_at', 'updated_at')
    inlines = [SaleItemInline]


@admin.register(SaleItem)
class SaleItemAdmin(admin.ModelAdmin):
    list_display = ('product', 'sale', 'price', 'discount_percent', 'stock_total', 'stock_remaining')
    list_filter = ('sale__status',)
    search_fields = ('product__name', 'sale__title')
    readonly_fields = ('id',)
