from django.contrib import admin
from .models import (
    Asset,
    AssetAssignment,
    AssetMaintenance,
    Branch,
    Category,
    Product,
    ProductBatch,
    ProductSerial,
    ProductStock,
    PurchaseOrder,
    PurchaseOrderItem,
    PurchaseReturn,
    StockAdjustmentRequest,
    StockLocation,
    StockMovement,
    StockTransfer,
    StockTransferItem,
    Supplier,
    UserStoreAssignment,
)


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name',)


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = (
        'name', 'sku', 'barcode', 'category', 'cost_price',
        'selling_price', 'stock_quantity', 'low_stock_alert', 'overstock_alert', 'is_active'
    )
    list_filter = ('category', 'is_active')
    search_fields = ('name', 'sku', 'barcode')


@admin.register(StockMovement)
class StockMovementAdmin(admin.ModelAdmin):
    list_display = (
        'product', 'location', 'movement_type', 'quantity',
        'reference', 'date', 'created_by'
    )
    list_filter = ('movement_type',)
    date_hierarchy = 'date'


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


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


@admin.register(PurchaseOrder)
class PurchaseOrderAdmin(admin.ModelAdmin):
    list_display = ('reference', 'supplier', 'total', 'status', 'date', 'received_date')
    list_filter = ('status',)
    inlines = [PurchaseOrderItemInline]
    date_hierarchy = 'date'


@admin.register(StockLocation)
class StockLocationAdmin(admin.ModelAdmin):
    list_display = ('name', 'branch', 'code', 'is_active')
    list_filter = ('branch', 'is_active')
    search_fields = ('name', 'code', 'branch__name')


@admin.register(Branch)
class BranchAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'location', 'contact_phone', 'is_active')
    list_filter = ('is_active',)
    search_fields = ('name', 'code', 'location')


@admin.register(UserStoreAssignment)
class UserStoreAssignmentAdmin(admin.ModelAdmin):
    list_display = ('user', 'location', 'is_active', 'assigned_at')
    list_filter = ('location', 'is_active')
    search_fields = ('user__username', 'location__name')


@admin.register(ProductStock)
class ProductStockAdmin(admin.ModelAdmin):
    list_display = ('product', 'location', 'quantity', 'updated_at')
    list_filter = ('location',)


@admin.register(ProductBatch)
class ProductBatchAdmin(admin.ModelAdmin):
    list_display = ('product', 'batch_number', 'location', 'expiry_date', 'quantity')
    list_filter = ('location',)
    search_fields = ('product__name', 'batch_number')


@admin.register(ProductSerial)
class ProductSerialAdmin(admin.ModelAdmin):
    list_display = ('serial_number', 'product', 'location', 'status', 'received_date', 'sold_date')
    list_filter = ('status', 'location')
    search_fields = ('serial_number', 'product__name')


class StockTransferItemInline(admin.TabularInline):
    model = StockTransferItem
    extra = 0


@admin.register(StockTransfer)
class StockTransferAdmin(admin.ModelAdmin):
    list_display = ('reference', 'from_location', 'to_location', 'status', 'requested_by', 'approved_by')
    list_filter = ('status', 'from_location', 'to_location')
    inlines = [StockTransferItemInline]


@admin.register(StockAdjustmentRequest)
class StockAdjustmentRequestAdmin(admin.ModelAdmin):
    list_display = ('product', 'location', 'quantity', 'reason', 'status', 'requested_by', 'approved_by')
    list_filter = ('reason', 'status')


@admin.register(PurchaseReturn)
class PurchaseReturnAdmin(admin.ModelAdmin):
    list_display = ('reference', 'purchase_order', 'status', 'date', 'processed_date')
    list_filter = ('status',)


@admin.register(Asset)
class AssetAdmin(admin.ModelAdmin):
    list_display = ('code', 'name', 'category', 'status', 'location', 'purchase_cost')
    list_filter = ('status', 'category')
    search_fields = ('code', 'name', 'serial_number')


@admin.register(AssetAssignment)
class AssetAssignmentAdmin(admin.ModelAdmin):
    list_display = ('asset', 'assigned_to', 'assigned_date', 'returned_date')


@admin.register(AssetMaintenance)
class AssetMaintenanceAdmin(admin.ModelAdmin):
    list_display = ('asset', 'description', 'cost', 'service_date', 'next_due_date')
