# inventory/admin.py
from django.contrib import admin
from .models import (
    ProductCategory, Product, InventoryTransaction, StockAdjustment,
    Supplier, PurchaseOrder, PurchaseOrderItem, SupplierPayment
)

@admin.register(ProductCategory)
class ProductCategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'description', 'color_code', 'created_at')
    list_filter = ('company', 'created_at')
    search_fields = ('name', 'description')
    ordering = ('company', 'name')

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'sku', 'category', 'price', 'stock_quantity', 'stock_status', 'status')
    list_filter = ('company', 'category', 'status', 'stock_status')
    search_fields = ('name', 'sku', 'barcode', 'company__name')
    readonly_fields = ('stock_status',)
    ordering = ('company', 'name')

@admin.register(InventoryTransaction)
class InventoryTransactionAdmin(admin.ModelAdmin):
    list_display = ('product', 'company', 'transaction_type', 'quantity', 'reference', 'created_by', 'created_at')
    list_filter = ('company', 'transaction_type', 'created_at', 'product__category')
    search_fields = ('product__name', 'reference', 'notes', 'company__name')
    ordering = ('company', '-created_at')

@admin.register(StockAdjustment)
class StockAdjustmentAdmin(admin.ModelAdmin):
    list_display = ('product', 'company', 'adjustment_type', 'quantity', 'performed_by', 'created_at')
    list_filter = ('company', 'adjustment_type', 'created_at')
    search_fields = ('product__name', 'reason', 'reference_number', 'company__name')
    ordering = ('company', '-created_at')

@admin.register(Supplier)
class SupplierAdmin(admin.ModelAdmin):
    list_display = ('name', 'company', 'phone', 'email', 'created_at')
    list_filter = ('company', 'created_at')
    search_fields = ('name', 'phone', 'email', 'company__name')
    ordering = ('company', 'name')

class PurchaseOrderItemInline(admin.TabularInline):
    model = PurchaseOrderItem
    extra = 0

@admin.register(PurchaseOrder)
class PurchaseOrderAdmin(admin.ModelAdmin):
    list_display = ('id', 'get_company', 'supplier', 'status', 'date_ordered', 'date_received', 'created_at')
    list_filter = ('supplier__company', 'status', 'date_ordered', 'date_received')
    search_fields = ('supplier__name', 'reference_number')
    inlines = [PurchaseOrderItemInline]
    ordering = ('supplier__company', '-created_at')

    def get_company(self, obj):
        return obj.supplier.company
    get_company.short_description = 'Company'
    get_company.admin_order_field = 'supplier__company'

@admin.register(SupplierPayment)
class SupplierPaymentAdmin(admin.ModelAdmin):
    list_display = ('supplier', 'get_company', 'amount', 'date')
    list_filter = ('supplier__company', 'date')
    search_fields = ('supplier__name', 'notes')
    ordering = ('supplier__company', '-date')

    def get_company(self, obj):
        return obj.supplier.company
    get_company.short_description = 'Company'
    get_company.admin_order_field = 'supplier__company'