from django.contrib import admin
from .models import DeviceType, DeviceBatch, Device, UserDevice

# Register your models here.
@admin.register(DeviceType)
class DeviceTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'code', 'created_at')
    search_fields = ('name', 'code')
    list_filter = ('created_at',)


@admin.register(DeviceBatch)
class DeviceBatchAdmin(admin.ModelAdmin):
    list_display = ('batch_number', 'device_type', 'production_date', 'quantity', 'created_at')
    search_fields = ('batch_number', 'device_type__name')
    list_filter = ('device_type', 'production_date', 'created_at')


@admin.register(Device)
class DeviceAdmin(admin.ModelAdmin):
    list_display = ('device_code', 'device_type', 'batch', 'serial_number', 'mac_address', 'status', 'battery_level', 'firmware_version', 'is_active', 'created_at')
    search_fields = ('device_code', 'serial_number', 'mac_address', 'firmware_version', 'wifi_name')
    list_filter = ('device_type', 'batch', 'status', 'is_active', 'created_at')
    fieldsets = (
        ('基本信息', {
            'fields': ('device_type', 'batch', 'serial_number', 'device_code', 'mac_address', 'is_active', 'activated_at')
        }),
        ('状态信息', {
            'fields': ('status', 'battery_level', 'firmware_version')
        }),
        ('网络设置', {
            'fields': ('wifi_name', 'wifi_password')
        }),
        ('设备设置', {
            'fields': ('brightness',)
        }),
    )


@admin.register(UserDevice)
class UserDeviceAdmin(admin.ModelAdmin):
    list_display = ('user', 'device', 'nickname', 'is_primary', 'bound_at')
    search_fields = ('user__username', 'device__device_code', 'nickname')
    list_filter = ('is_primary', 'bound_at')
