from django.contrib import admin
from .models import Device
from .models import (
    Category, Unit, Product, Customer, Supplier, 
    Sale, SaleItem, SalePayment, 
    Purchase, PurchaseItem, PurchasePayment,
    Quotation, QuotationItem,
    SaleReturn, SaleReturnItem,
    PurchaseReturn, PurchaseReturnItem,
    SystemSetting, PaymentMethod, HeldSale,
    LoyaltyTier, LoyaltyTransaction
)

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name_en', 'name_ar', 'slug')
    prepopulated_fields = {'slug': ('name_en',)}

@admin.register(Unit)
class UnitAdmin(admin.ModelAdmin):
    list_display = ('name_en', 'name_ar', 'short_name')

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ('name_en', 'name_ar', 'sku', 'price', 'stock_quantity', 'category', 'unit')
    list_filter = ('category', 'unit', 'is_active')
    search_fields = ('name_en', 'name_ar', 'sku')

@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
    list_display = ('name', 'phone', 'email', 'loyalty_points', 'loyalty_tier')
    search_fields = ('name', 'phone')

@admin.register(Supplier)
class SupplierAdmin(admin.ModelAdmin):
    list_display = ('name', 'contact_person', 'phone')

@admin.register(PaymentMethod)
class PaymentMethodAdmin(admin.ModelAdmin):
    list_display = ('name_en', 'name_ar', 'is_active')

class SaleItemInline(admin.TabularInline):
    model = SaleItem
    extra = 1

class SalePaymentInline(admin.TabularInline):
    model = SalePayment
    extra = 1

@admin.register(Sale)
class SaleAdmin(admin.ModelAdmin):
    list_display = ('id', 'invoice_number', 'customer', 'total_amount', 'paid_amount', 'status', 'created_at')
    list_filter = ('status', 'created_at')
    inlines = [SaleItemInline, SalePaymentInline]

@admin.register(Purchase)
class PurchaseAdmin(admin.ModelAdmin):
    list_display = ('id', 'invoice_number', 'supplier', 'total_amount', 'paid_amount', 'status', 'created_at')
    list_filter = ('supplier', 'status', 'created_at')

@admin.register(Quotation)
class QuotationAdmin(admin.ModelAdmin):
    list_display = ('quotation_number', 'customer', 'total_amount', 'status', 'created_at')
    list_filter = ('status', 'created_at')

@admin.register(SaleReturn)
class SaleReturnAdmin(admin.ModelAdmin):
    list_display = ('return_number', 'customer', 'total_amount', 'created_at')

@admin.register(PurchaseReturn)
class PurchaseReturnAdmin(admin.ModelAdmin):
    list_display = ('return_number', 'supplier', 'total_amount', 'created_at')

@admin.register(SystemSetting)
class SystemSettingAdmin(admin.ModelAdmin):
    list_display = ('business_name', 'phone', 'email', 'vat_number')

@admin.register(HeldSale)
class HeldSaleAdmin(admin.ModelAdmin):
    list_display = ('id', 'customer', 'total_amount', 'created_at')

@admin.register(LoyaltyTier)
class LoyaltyTierAdmin(admin.ModelAdmin):
    list_display = ('name_en', 'name_ar', 'min_points', 'point_multiplier', 'discount_percentage')

@admin.register(LoyaltyTransaction)
class LoyaltyTransactionAdmin(admin.ModelAdmin):
    list_display = ('customer', 'transaction_type', 'points', 'created_at')
    list_filter = ('transaction_type', 'created_at')
    search_fields = ('customer__name',)

@admin.register(Device)
class DeviceAdmin(admin.ModelAdmin):
    list_display = ('name', 'device_type', 'connection_type', 'ip_address', 'is_active')
    list_filter = ('device_type', 'connection_type', 'is_active')
    search_fields = ('name', 'ip_address')
