"""
仪表盘视图 - 统计数据
"""
from rest_framework import viewsets
from rest_framework.decorators import action

from utils.response import success
from apps.admins.authentication import AdminJWTAuthentication
from apps.admins.permissions import IsAdminUser
from apps.users.models import User
from apps.devices.models import DeviceType, DeviceBatch, Device


class DashboardViewSet(viewsets.ViewSet):
    """仪表盘统计视图集"""
    
    authentication_classes = [AdminJWTAuthentication]
    permission_classes = [IsAdminUser]
    
    @action(detail=False, methods=['get'])
    def stats(self, request):
        """
        获取仪表盘统计数据
        GET /api/admin/dashboard/stats
        """
        # 用户总数
        user_count = User.objects.count()
        
        # 设备类型数
        device_type_count = DeviceType.objects.count()
        
        # 批次数
        batch_count = DeviceBatch.objects.count()
        
        # 设备统计
        device_count = Device.objects.count()
        bound_device_count = Device.objects.filter(status='bound').count()
        in_stock_device_count = Device.objects.filter(status='in_stock').count()
        
        return success(data={
            'user_count': user_count,
            'device_count': device_count,
            'device_type_count': device_type_count,
            'batch_count': batch_count,
            'bound_device_count': bound_device_count,
            'in_stock_device_count': in_stock_device_count,
        })
