from django.contrib import admin
from .models import (
    Category, Product, ProductImage, Order, OrderItem, 
    Carrier, Payment, DiscountCode, Refund, Invoice, Cart, CartItem, Wishlist
)


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ("name", "url", "parent")
    search_fields = ("name", "description")
    prepopulated_fields = {"url": ("name",)}


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ("name", "price", "stock", "is_active", "category", "created_at")
    search_fields = ("name", "description", "code")
    list_filter = ("is_active", "category", "created_at")
    prepopulated_fields = {"url": ("name",)}


@admin.register(ProductImage)
class ProductImageAdmin(admin.ModelAdmin):
    list_display = ("product", "is_main", "alt_text")
    list_filter = ("is_main",)
    search_fields = ("product__name", "alt_text")


class OrderItemInline(admin.TabularInline):
    model = OrderItem
    extra = 0
    readonly_fields = ("product", "quantity")


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ("id", "user", "email", "status", "total_price", "currency", "created_at")
    list_filter = ("status", "created_at", "country")
    search_fields = ("email", "first_name", "last_name", "phone")
    readonly_fields = ("created_at", "updated_at", "total_price")
    inlines = [OrderItemInline]


@admin.register(OrderItem)
class OrderItemAdmin(admin.ModelAdmin):
    list_display = ("order", "product", "quantity")
    search_fields = ("order__id", "product__name")


@admin.register(Carrier)
class CarrierAdmin(admin.ModelAdmin):
    list_display = ("id", "shipping_method", "state", "shipping_price", "weight")
    list_filter = ("shipping_method", "state", "returning")
    search_fields = ("id",)


@admin.register(Payment)
class PaymentAdmin(admin.ModelAdmin):
    list_display = ("id", "payment_method", "created_at")
    list_filter = ("payment_method", "created_at")


@admin.register(DiscountCode)
class DiscountCodeAdmin(admin.ModelAdmin):
    list_display = ("code", "percent", "amount", "active", "valid_from", "valid_to", "used_count", "usage_limit")
    list_filter = ("active", "valid_from", "valid_to")
    search_fields = ("code", "description")


@admin.register(Refund)
class RefundAdmin(admin.ModelAdmin):
    list_display = ("order", "reason_choice", "verified", "created_at")
    list_filter = ("verified", "reason_choice", "created_at")
    search_fields = ("order__id", "order__email", "reason_text")


@admin.register(Invoice)
class InvoiceAdmin(admin.ModelAdmin):
    list_display = ("invoice_number", "issued_at", "due_date")
    search_fields = ("invoice_number",)
    readonly_fields = ("issued_at",)


class CartItemInline(admin.TabularInline):
    model = CartItem
    extra = 0
    readonly_fields = ("product", "quantity", "added_at")


@admin.register(Cart)
class CartAdmin(admin.ModelAdmin):
    list_display = ("id", "user", "session_key", "created_at", "updated_at")
    list_filter = ("created_at", "updated_at")
    search_fields = ("user__email", "session_key")
    readonly_fields = ("created_at", "updated_at")
    inlines = [CartItemInline]


@admin.register(CartItem)
class CartItemAdmin(admin.ModelAdmin):
    list_display = ("cart", "product", "quantity", "added_at")
    list_filter = ("added_at",)
    search_fields = ("cart__id", "product__name")
    readonly_fields = ("added_at",)


@admin.register(Wishlist)
class WishlistAdmin(admin.ModelAdmin):
    list_display = ("user", "product_count", "created_at", "updated_at")
    search_fields = ("user__email", "user__username")
    readonly_fields = ("created_at", "updated_at")
    filter_horizontal = ("products",)
    
    def product_count(self, obj):
        return obj.products.count()
    product_count.short_description = "Products Count"
