from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import Apartament
from .serializer import (ApartamentListSerializer, 
                         ApartamentDetailSerializer)


class ApartamentViewSet(viewsets.ReadOnlyModelViewSet):
    """Вывод списка квартир или отдельной квартиры"""

    def get_queryset(self):
        apartaments = Apartament.objects.all()
        print(apartaments)
        return apartaments
    
    def get_serializer_class(self):
        if self.action == 'list':
            return ApartamentListSerializer
        elif self.action == "retrieve":
            return ApartamentDetailSerializer


class ApartamentComparisonViewSet(viewsets.ReadOnlyModelViewSet):
    """Вывод отдельных квартир для сравнения"""
    def retrieve(self, request, *args, **kwargs):
        apartaments_id = request.COOKIES["apartament_comparison_id"].split(',') # получение куки пример (123,453)
        apartaments = []
        for i in apartaments_id:
            apartaments.append(ApartamentDetailSerializer(Apartament.objects.get(pk=i)).data)
        return Response({"results": apartaments})