from django.contrib.auth.decorators import permission_required
from rest_framework import routers, viewsets
from django.urls import re_path
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from notifications.models import MessageTemplate, UserNotificationChannel
from rest_framework import serializers

from notifications.templates import TEMPLATE_VARS
from authentication.serializers import UserSerializer


class MessageTemplateSerializer(serializers.ModelSerializer):
    class Meta:
        model = MessageTemplate
        fields = '__all__'


class UserNotificationChannelSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = UserNotificationChannel
        fields = '__all__'


class MessageTemplateViewSet(viewsets.ModelViewSet):
    serializer_class = MessageTemplateSerializer
    queryset = MessageTemplate.objects.all()


class UserNotificationChannelViewSet(viewsets.ModelViewSet):
    serializer_class = UserNotificationChannelSerializer
    queryset = UserNotificationChannel.objects.all()


@api_view(['GET'])
@permission_classes([IsAuthenticated])
@permission_required('tickets.add_issuethread_manual', raise_exception=True)  # TDOO: change this permission
def get_template_vars(self):
    return Response(TEMPLATE_VARS, status=200)


router = routers.SimpleRouter()
router.register(r'message_templates', MessageTemplateViewSet)
router.register(r'user_notification_channels', UserNotificationChannelViewSet)
urlpatterns = ([
                   re_path('message_template_variables', get_template_vars),
               ] + router.urls)