"""
Django admin configuration for deposits app.
"""
from django.contrib import admin
from .models import Deposit, DepositStatus, PaymentInfo
from .services import apply_deposit_approval_effects, reverse_deposit_approval_effects


@admin.register(Deposit)
class DepositAdmin(admin.ModelAdmin):
    """
    Admin configuration for Deposit model.
    """
    list_display = ('user', 'amount', 'payment_method', 'transaction_id', 'status', 'verified_by', 'created_at')
    list_filter = ('status', 'payment_method')
    search_fields = ('user__username', 'transaction_id', 'wallet_address')
    readonly_fields = ('created_at', 'updated_at')
    list_editable = ('status',)
    
    def save_model(self, request, obj, form, change):
        if change:
            # Get the old object to check status change
            old_obj = Deposit.objects.get(pk=obj.pk)
            
            # If status changed to APPROVED
            if obj.status == DepositStatus.APPROVED and old_obj.status != DepositStatus.APPROVED:
                obj.verified_by = request.user
                from django.utils import timezone
                obj.verified_at = timezone.now()
        
        super().save_model(request, obj, form, change)

        if change:
            if obj.status == DepositStatus.APPROVED and old_obj.status != DepositStatus.APPROVED:
                apply_deposit_approval_effects(obj)
            elif obj.status != DepositStatus.APPROVED and old_obj.status == DepositStatus.APPROVED:
                reverse_deposit_approval_effects(obj)


@admin.register(PaymentInfo)
class PaymentInfoAdmin(admin.ModelAdmin):
    """
    Admin configuration for PaymentInfo model.
    """
    list_display = ('payment_method', 'wallet_address', 'minimum_amount', 'is_active', 'created_at')
    list_filter = ('is_active', 'payment_method')
    list_editable = ('is_active',)
    search_fields = ('payment_method', 'wallet_address')
