from rest_framework import status
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema, OpenApiResponse, OpenApiExample

from api.account.serializers.sensor_serializers import DetectorsResponseSerializer
from sitemanagement.models import Sensor

from api.utils.decorators import handle_exceptions

@extend_schema(tags=['Датчики'])
class SensorView(APIView):
    permission_classes = [IsAuthenticated]
    serializer_class = DetectorsResponseSerializer
    
    @extend_schema(
        summary="Получение всех датчиков",
        description="Получение всех датчиков в формате детекторов",
        responses={200: OpenApiResponse(response=DetectorsResponseSerializer, description="Датчики успешно получены", 
                                        examples=[OpenApiExample(
                                            'Успешный ответ',
                                            value={
                                                
                                                    "id": 1,
                                                    "type": "fire_detector",
                                                    "name": "Датчик 1",
                                                    "object": "Объект 1",
                                                    "status": "warning",
                                                    "zone": "Зона 1",
                                                    "serial_number": "GA-123",
                                                    "floor": 1,
                                                    "notifications": [
                                                        {
                                                            "id": 1,
                                                            "type": "warning",
                                                            "timestamp": "2024-01-15T14:30:00Z",
                                                            "acknowledged": False,
                                                            "priority": "high"
                                                        }
                                                    ]
                                            }
                                        )])})
    @handle_exceptions
    def get(self, request):
        """Получение всех датчиков"""
        try:
            sensors = Sensor.objects.select_related(
                'sensor_type',
                'signal_format'
            ).prefetch_related(
                'zones',
                'zones__object',
                'alerts'
            ).all()
            
            serializer = DetectorsResponseSerializer(sensors)
            return Response(serializer.data, status=status.HTTP_200_OK)
        except Sensor.DoesNotExist:
            return Response(
                {"error": "Датчики не найдены"}, 
                status=status.HTTP_404_NOT_FOUND)