from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from .models import Item, Quote, QuoteItem, Invoice, InvoiceItem, Payment

@admin.register(Item)
class ItemAdmin(SimpleHistoryAdmin):
    list_display = ['name', 'unit_price', 'default_quantity', 'is_default_in_quotes', 'is_special', 'is_active', 'created_by']
    list_filter = ['is_default_in_quotes', 'is_special', 'is_active', 'created_by']
    search_fields = ['name', 'description']
    prepopulated_fields = {'slug': ('name',)}
    readonly_fields = ['deleted_at', 'created', 'updated']

class QuoteItemInline(admin.TabularInline):
    model = QuoteItem
    extra = 1
    fields = ['item', 'quantity', 'unit_price', 'total_price', 'notes']

@admin.register(Quote)
class QuoteAdmin(SimpleHistoryAdmin):
    list_display = ['name', 'process_instance', 'customer', 'status_display', 'total_amount', 'final_amount', 'valid_until', 'created_by']
    list_filter = ['status', 'created', 'valid_until', 'process_instance__process']
    search_fields = ['name', 'customer__username', 'customer__first_name', 'customer__last_name', 'notes']
    prepopulated_fields = {'slug': ('name',)}
    readonly_fields = ['deleted_at', 'created', 'updated', 'total_amount', 'discount_amount', 'final_amount']
    inlines = [QuoteItemInline]
    ordering = ['-created']

    def status_display(self, obj):
        return mark_safe(obj.get_status_display_with_color())
    status_display.short_description = "وضعیت"

class InvoiceItemInline(admin.TabularInline):
    model = InvoiceItem
    extra = 1
    fields = ['item', 'quantity', 'unit_price', 'total_price', 'notes']

class PaymentInline(admin.TabularInline):
    model = Payment
    extra = 1
    fields = ['amount', 'payment_method', 'reference_number', 'payment_date', 'notes']
    readonly_fields = ['created_by', 'created']

@admin.register(Invoice)
class InvoiceAdmin(SimpleHistoryAdmin):
    list_display = ['name', 'process_instance', 'customer', 'status_display', 'final_amount', 'paid_amount_display', 'remaining_amount_display', 'due_date']
    list_filter = ['status', 'created', 'due_date', 'process_instance__process']
    search_fields = ['name', 'customer__username', 'customer__first_name', 'customer__last_name', 'notes']
    prepopulated_fields = {'slug': ('name',)}
    readonly_fields = ['deleted_at', 'created', 'updated', 'total_amount', 'discount_amount', 'final_amount', 'paid_amount_display', 'remaining_amount_display']
    inlines = [InvoiceItemInline, PaymentInline]
    ordering = ['-created']

    def status_display(self, obj):
        return mark_safe(obj.get_status_display_with_color())
    status_display.short_description = "وضعیت"

    def paid_amount_display(self, obj):
        return f"{obj.get_paid_amount():,.0f} ریال"
    paid_amount_display.short_description = "مبلغ پرداخت شده"

    def remaining_amount_display(self, obj):
        amount = obj.get_remaining_amount()
        color = "green" if amount <= 0 else "red"
        return format_html('<span style="color: {};">{:,.0f} ریال</span>', color, amount)
    remaining_amount_display.short_description = "مبلغ باقی‌مانده"

@admin.register(Payment)
class PaymentAdmin(SimpleHistoryAdmin):
    list_display = ['invoice', 'amount', 'payment_method', 'payment_date', 'created_by']
    list_filter = ['payment_method', 'payment_date', 'created_by']
    search_fields = ['invoice__name', 'reference_number', 'notes']
    readonly_fields = ['created']
    ordering = ['-payment_date']
