from django.contrib import admin
from .models import Supplier, PurchaseOrder, PurchaseOrderItem, PurchaseReceipt

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

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

@admin.register(PurchaseOrder)
class PurchaseOrderAdmin(admin.ModelAdmin):
    list_display = ('order_number', 'supplier', 'date', 'expected_delivery_date', 
                   'status', 'total_amount', 'created_by', 'created_at')
    list_filter = ('status', 'date', 'expected_delivery_date', 'supplier', 'created_by', 'created_at')
    search_fields = ('order_number', 'supplier__name', 'supplier__code', 'notes')
    ordering = ('-date', '-created_at')
    
    fieldsets = (
        ('Order Information', {'fields': ('order_number', 'supplier', 'date', 'expected_delivery_date', 'status')}),
        ('Financial Information', {'fields': ('subtotal', 'tax_amount', 'shipping_cost', 'total_amount')}),
        ('Additional Information', {'fields': ('notes', 'terms_conditions', 'created_by')}),
    )
    
    readonly_fields = ('subtotal', 'total_amount')
    inlines = [PurchaseOrderItemInline]
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('supplier', 'created_by')

@admin.register(PurchaseOrderItem)
class PurchaseOrderItemAdmin(admin.ModelAdmin):
    list_display = ('purchase_order', 'product', 'quantity', 'unit_price', 
                   'total_price', 'description')
    list_filter = ('purchase_order__status', 'purchase_order__date')
    search_fields = ('purchase_order__order_number', 'product__name', 'product__code')
    ordering = ('-purchase_order__date',)
    
    fieldsets = (
        ('Item Information', {'fields': ('purchase_order', 'product', 'quantity', 'unit_price')}),
        ('Additional Information', {'fields': ('description', 'notes')}),
    )
    
    readonly_fields = ('total_price',)
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('purchase_order', 'product')

@admin.register(PurchaseReceipt)
class PurchaseReceiptAdmin(admin.ModelAdmin):
    list_display = ('receipt_number', 'purchase_order', 'receipt_date', 'received_by', 'created_at')
    list_filter = ('receipt_date', 'received_by', 'created_at')
    search_fields = ('receipt_number', 'purchase_order__order_number')
    ordering = ('-receipt_date',)
    
    fieldsets = (
        ('Receipt Information', {'fields': ('receipt_number', 'purchase_order', 'receipt_date')}),
        ('Additional Information', {'fields': ('notes', 'received_by')}),
    )
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('purchase_order', 'received_by')
