from apps.authentication.api.v1.serializers.serializer import (
    CitySerializer,
    ProvinceSerializer
)
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
from apps.authentication.models import City, Province
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework import status


class CityViewSet(ModelViewSet, SoftDeleteMixin):  # noqa
    """ Crud operations for city model """  #
    queryset = City.objects.all()
    serializer_class = CitySerializer
    permission_classes = [AllowAny]

    def list(self, request, *args, **kwargs):
        """ return list of cities by province """
        if 'province' in self.request.query_params.keys():
            query = self.queryset.filter(province_id=int(request.GET['province']))
        else:
            query = self.queryset

        serializer = self.serializer_class(query, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)


class ProvinceViewSet(ModelViewSet, SoftDeleteMixin):
    """ Crud operations for province model """  #
    queryset = Province.objects.all()
    serializer_class = ProvinceSerializer
    permission_classes = [AllowAny]
