"""
Billing Module Admin
"""
from django.contrib import admin
from igny8_core.admin.base import AccountAdminMixin
from .models import CreditTransaction, CreditUsageLog


@admin.register(CreditTransaction)
class CreditTransactionAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['id', 'account', 'transaction_type', 'amount', 'balance_after', 'description', 'created_at']
    list_filter = ['transaction_type', 'created_at', 'account']
    search_fields = ['description', 'account__name']
    readonly_fields = ['created_at']
    date_hierarchy = 'created_at'
    
    def get_account_display(self, obj):
        """Safely get account name"""
        try:
            account = getattr(obj, 'account', None)
            return account.name if account else '-'
        except:
            return '-'
    get_account_display.short_description = 'Account'


@admin.register(CreditUsageLog)
class CreditUsageLogAdmin(AccountAdminMixin, admin.ModelAdmin):
    list_display = ['id', 'account', 'operation_type', 'credits_used', 'cost_usd', 'model_used', 'created_at']
    list_filter = ['operation_type', 'created_at', 'account', 'model_used']
    search_fields = ['account__name', 'model_used']
    readonly_fields = ['created_at']
    date_hierarchy = 'created_at'
    
    def get_account_display(self, obj):
        """Safely get account name"""
        try:
            account = getattr(obj, 'account', None)
            return account.name if account else '-'
        except:
            return '-'
    get_account_display.short_description = 'Account'

