from rest_framework import serializers
from django.conf import settings
from drf_spectacular.utils import extend_schema_field
from drf_spectacular.types import OpenApiTypes
from typing import Optional
from sitemanagement.models import Object, Zone, Sensor

class SensorBasicSerializer(serializers.ModelSerializer):
    class Meta:
        model = Sensor
        fields = ('id', 'name', 'serial_number', 'sensor_type')

class ZoneSerializer(serializers.ModelSerializer):
    sensors = SensorBasicSerializer(many=True, read_only=True)
    
    class Meta:
        model = Zone
        fields = ('id', 'name', 'floor', 'image_path', 'model_path', 'order', 'sensors')

class ObjectSerializer(serializers.ModelSerializer):
    zones = ZoneSerializer(many=True, read_only=True)
    image = serializers.SerializerMethodField()
    
    class Meta:
        model = Object
        fields = ('id', 'title', 'description', 'image', 'address', 'floors', 'area', 'zones')

    @extend_schema_field(OpenApiTypes.URI)
    def get_image(self, obj) -> Optional[str]:
        """Возвращает URL изображения объекта"""
        return f"{settings.BASE_URL}{obj.image.url}" if obj.image else None