from django.contrib import admin
from .models import Expense


class ExpenseAdmin(admin.ModelAdmin):
    list_display = [
        'title', 'category', 'amount', 'payment_method', 
        'branch', 'staff', 'status', 'expense_date', 'created_at'
    ]
    list_filter = [
        'status', 'category', 'payment_method', 'branch', 
        'expense_date', 'created_at'
    ]
    search_fields = [
        'title', 'description', 'paid_to', 'reference_number'
    ]
    readonly_fields = ['created_at', 'updated_at', 'approved_at']
    date_hierarchy = 'expense_date'
    
    fieldsets = (
        ('Basic Information', {
            'fields': ('title', 'description', 'category')
        }),
        ('Financial Details', {
            'fields': ('amount', 'payment_method', 'paid_to', 'reference_number')
        }),
        ('Relationships', {
            'fields': ('branch', 'staff', 'loan')
        }),
        ('Documentation', {
            'fields': ('receipt_path', 'notes')
        }),
        ('Approval', {
            'fields': ('status', 'approved_by', 'approved_at', 'rejection_reason')
        }),
        ('Dates', {
            'fields': ('expense_date', 'created_at', 'updated_at')
        }),
    )
    
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related('branch', 'staff', 'loan', 'approved_by')


admin.site.register(Expense, ExpenseAdmin)
