from django.shortcuts import render
from datetime import datetime, timedelta
import pytz, random

from .models import *
from .serializers import *

from django.db.models import Q

from rest_framework import generics
from rest_framework.decorators import authentication_classes, permission_classes
from rest_framework.authentication import SessionAuthentication, BasicAuthentication

from rest_framework.permissions import IsAuthenticated
# from durin.auth import TokenAuthentication

# from durin.views import APIAccessTokenView

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters

from rest_framework.response import Response
from rest_framework_api_key.permissions import HasAPIKey

td = timedelta(hours=7)
odt = datetime.now() - td

# Create your views here.
class EventsAPIView(generics.ListAPIView):
    serializer_class = EventSerializer
    queryset = Event.objects.filter(show_date__gte=odt).order_by('show_date')
    filter_backends = [DjangoFilterBackend, filters.SearchFilter]
    filterset_fields = ['show_title', 'event_type', 'venue__name', 'calendar__shortcode']
    search_fields = ['show_title', 'event_type', 'venue__name']
    permission_classes = [HasAPIKey]


class PromoAPIView(generics.ListAPIView):
    serializer_class = PromoSerializer
    permission_classes = [HasAPIKey]
    
    def get_queryset(self):
        promo_objects = list(Promo.objects.filter(published=True))
        print(len(promo_objects))
        queryset = random.sample(promo_objects, 21)
        return queryset

# class EventsTokenAPIView(APIAccessTokenView):
#     serializer_class = EventSerializer
#     authentication_classes = (TokenAuthentication, BasicAuthentication,)
#     permission_classes = (IsAuthenticated,)
#     queryset = Event.objects.filter(show_date__gte=odt).order_by('show_date')
#     filter_backends = [DjangoFilterBackend, filters.SearchFilter]
#     filterset_fields = ['show_title', 'event_type', 'show_date', 'show_day', 'venue__name']
#     search_fields = ['show_title', 'event_type']
