from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from backoffice.models import *
from rest_framework import serializers
from rest_framework import filters
from rest_framework.pagination import PageNumberPagination
from django.db.models import F,Q
from django.db.models import Sum
from drf_extra_fields.fields import Base64ImageField

class UserUpdateSerializer(ModelSerializer):
    pinCode = serializers.CharField(max_length=4, min_length=4, required=False,help_text="4 digit number pin code")
    class Meta:
        model = User
        ref_name = "selltix_user"
        fields = ('id','first_name','last_name','phoneNumber','pinCode','cryptoAccepted','maticAccepted','usdtAccepted','usdcAccepted','cbAccepted',
                  'organizerCompany','organizeWebsite','webhookUrl','country','timezone')
    

class UserReadSerializer(ModelSerializer):
    
    class Meta:
        model = User
        ref_name = "selltix_user"
        exclude =  ('isOrganizer','generatedWallet','password','last_login','transactionHash','signInToken','stripeCustomerId','accountLocked','appleGoogleWallet','qrcodeTimeout','maticSent','groups','user_permissions','privateKey','words','publicKey','pinCode','is_staff','is_superuser','is_active','lastLoginCode','lastLoginCodeDate','nonce',)
        read_only_fields = ['is_staff','is_superuser','is_active','user_permissions']

class EventsForCreateCustomSerializer(ModelSerializer):
    image = Base64ImageField(required=False) 
    class Meta:
        model = Event
        exclude = ('refOrganiser','status','refEventPageTemplate','eventType','transactionHash','slug','venueName','venueAddress',
                   'venueGeoloc','previewVideo','webHookSet','entryTime','sellTixRoyaltieValue','appleGoogleWallet','qrcodeTimeout',
                   'streamingQuality','recordStreaming','recordedFile','mailRappelSent','streamingId','recordedStreamingUrl',
                   'eventContract','ticketContract','paymentContract','ticketTypeContract',)
    
     
    

class EventsCustomSerializer(ModelSerializer):
    sellTixEventPage = serializers.SerializerMethodField()
    class Meta:
        model = Event
        exclude = ('refOrganiser','status','refEventPageTemplate','eventType','transactionHash','slug','venueName','venueAddress',
                   'venueGeoloc','previewVideo','webHookSet','entryTime', 'sellTixRoyaltieValue','appleGoogleWallet','qrcodeTimeout',
                   'streamingQuality','recordStreaming','recordedFile','mailRappelSent','streamingId','recordedStreamingUrl',
                   'eventContract','ticketContract','paymentContract','ticketTypeContract',)
        read_only = ('status',)

    def get_sellTixEventPage(self,obj):
        return "https://www.selltix.live/event/"+obj.slug
    
    #TODO specific for video 
    """
    def to_representation(self, instance):
        representation = super().to_representation(instance)
        if instance.recordedVideo:
            representation['recordedStreamingUrl'] = instance.recordedStreamingUrl
        return representation
    """
class EventsForGetSerializer(ModelSerializer):
    sellTixEventPage = serializers.SerializerMethodField() 
    class Meta:
        model = Event
        exclude = ('refOrganiser', 'refEventPageTemplate','eventType','transactionHash','slug','venueName','venueAddress',
                   'venueGeoloc','previewVideo','webHookSet','entryTime', 'sellTixRoyaltieValue','appleGoogleWallet','qrcodeTimeout',
                   'streamingQuality','recordStreaming','recordedFile','mailRappelSent','streamingId','recordedStreamingUrl',
                   'eventContract','ticketContract','paymentContract','ticketTypeContract',)
    
    def get_sellTixEventPage(self,obj):

        return "https://www.selltix.live/event/"+obj.slug

class TicketTypeSerializerForCreate(ModelSerializer):
    image = Base64ImageField(required=False)
    class Meta:
        model = TicketType
        fields = ('refEvent','image', 'name','maxTickets','maxTicketsPerUser','ticketPrice','bookingStartDate','bookingEndDate',
                  'description','revealed','revealStartDate','hiddenuri','sellable','maxSellablePrice','royaltySellable',
                  'earlyBid', 'discountPrice','discountEndDate','canStream')

class TicketTypeForReadSerializer(ModelSerializer):
    class Meta:
        model = TicketType
        fields = ('id','refEvent','name','maxTickets','maxTicketsPerUser','ticketPrice','bookingStartDate','bookingEndDate',
                  'description','revealed','revealStartDate','hiddenuri','sellable','maxSellablePrice','royaltySellable',
                  'earlyBid', 'discountPrice','discountEndDate','canStream')