from django.shortcuts import render
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet, ListModelMixin
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin
from .serializers import DrainSerializer, DrainEquipSerializer, DrainEquipEnvSerializer
from .models import Drain, DrainEquip
from rest_framework.decorators import action
from apps.utils.sql import query_all_dict
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
# Create your views here.


class DrainViewSet(CustomModelViewSet):
    """
    list: 排口/污染源

    排口/污染源
    """
    queryset = Drain.objects.all()
    serializer_class = DrainSerializer
    filterset_fields = ['type', 'cate', 'mgroup']


class DrainEquipViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
    """
    list: 排口/设备关系

    排口/设备关系
    """
    perms_map = {'get': '*', 'post:': 'drain.update', 'delete': 'drain.update'}
    queryset = DrainEquip.objects.all()
    serializer_class = DrainEquipSerializer
    filterset_fields = ['drain', 'equipment',
                        'drain__mgroup', 'equipment__mgroup', 'drain__type']

    def get_serializer_class(self):
        has_envdata = self.request.query_params.get('has_envdata', 'no')
        if has_envdata == 'yes':
            return DrainEquipEnvSerializer
        return super().get_serializer_class()

    @swagger_auto_schema(manual_parameters=[
        openapi.Parameter(name="has_envdata", in_=openapi.IN_QUERY, description="Include environmental data in the response",
                          type=openapi.TYPE_STRING, enum=["yes", "no"], required=False),
    ])
    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)
