from django.contrib import admin
from .models import Customer, SaleOrder, SaleOrderItem, DeliveryNote

class SaleOrderItemInline(admin.TabularInline):
    model = SaleOrderItem
    extra = 1
    fields = ('product', 'quantity', 'unit_price', 'discount_percent', 'total_price', 'description', 'notes')

@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'customer_type', 'contact_person', 'email', 'phone', 
                   'rating', 'is_active', 'created_at')
    list_filter = ('customer_type', 'is_active', 'rating', 'created_at')
    search_fields = ('name', 'code', 'contact_person', 'email', 'phone')
    ordering = ('name',)
    
    fieldsets = (
        ('Basic Information', {'fields': ('name', 'code', 'customer_type', 'contact_person', 'email', 'phone')}),
        ('Address', {'fields': ('address',)}),
        ('Business Information', {'fields': ('tax_id', 'payment_terms', 'credit_limit')}),
        ('Status', {'fields': ('is_active', 'rating')}),
    )

@admin.register(SaleOrder)
class SaleOrderAdmin(admin.ModelAdmin):
    list_display = ('order_number', 'customer', 'date', 'expected_delivery_date', 
                   'status', 'total_amount', 'created_by', 'created_at')
    list_filter = ('status', 'date', 'expected_delivery_date', 'customer__customer_type', 'created_by', 'created_at')
    search_fields = ('order_number', 'customer__name', 'customer__code', 'notes')
    ordering = ('-date', '-created_at')
    
    fieldsets = (
        ('Order Information', {'fields': ('order_number', 'customer', 'date', 'expected_delivery_date', 'status')}),
        ('Financial Information', {'fields': ('subtotal', 'tax_amount', 'discount_amount', 'shipping_cost', 'total_amount')}),
        ('Additional Information', {'fields': ('notes', 'terms_conditions', 'created_by')}),
    )
    
    readonly_fields = ('subtotal', 'total_amount')
    inlines = [SaleOrderItemInline]
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('customer', 'created_by')

@admin.register(SaleOrderItem)
class SaleOrderItemAdmin(admin.ModelAdmin):
    list_display = ('sale_order', 'product', 'quantity', 'unit_price', 'discount_percent', 
                   'total_price', 'description')
    list_filter = ('sale_order__status', 'sale_order__date', 'product__category')
    search_fields = ('sale_order__order_number', 'product__name', 'product__code')
    ordering = ('-sale_order__date',)
    
    fieldsets = (
        ('Item Information', {'fields': ('sale_order', 'product', 'quantity', 'unit_price')}),
        ('Pricing', {'fields': ('discount_percent', 'total_price')}),
        ('Additional Information', {'fields': ('description', 'notes')}),
    )
    
    readonly_fields = ('total_price',)
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('sale_order', 'product')

@admin.register(DeliveryNote)
class DeliveryNoteAdmin(admin.ModelAdmin):
    list_display = ('delivery_number', 'sale_order', 'delivery_date', 'shipping_method', 
                   'tracking_number', 'delivered_by', 'created_at')
    list_filter = ('delivery_date', 'shipping_method', 'delivered_by', 'created_at')
    search_fields = ('delivery_number', 'sale_order__order_number', 'tracking_number')
    ordering = ('-delivery_date',)
    
    fieldsets = (
        ('Delivery Information', {'fields': ('delivery_number', 'sale_order', 'delivery_date')}),
        ('Shipping Details', {'fields': ('shipping_method', 'tracking_number')}),
        ('Additional Information', {'fields': ('notes', 'delivered_by')}),
    )
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('sale_order', 'delivered_by')
