"""
Copyright 2017 Peter Urda

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from rest_framework import viewsets
from rest_framework.decorators import detail_route
from rest_framework.response import Response

from django_discord.models import (
    GuildMembership,
    User,
)
from ..serializers import (
    UserGuildSerializer,
    UserSerializer,
)


class UserViewSet(viewsets.ModelViewSet):
    """ViewSet for Discord Users"""

    queryset = User.objects.all().order_by('id')

    serializer_class = UserSerializer

    http_method_names = [
        'get',
        'post',
        'patch',
        'head',
        'options',
    ]

    @detail_route()
    def guilds(self, request, pk):
        queryset = GuildMembership.objects.filter(user__id=pk).order_by('id')
        serializer = UserGuildSerializer(
            queryset,
            context={'request': request},
            many=True,
        )
        return Response(serializer.data)
