import datetime

from django.db.models import Q

from rest_framework import serializers, viewsets
from rest_framework.fields import SerializerMethodField
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from gasolinestation.models import FuelPrices, TypeOfFuel, GasolineStation


class FuelPriceSerializer(serializers.ModelSerializer):
    gas_station_assigned = serializers.SlugRelatedField(slug_field="name", queryset=GasolineStation.objects.all(), allow_null=True, required=False)

    class Meta:
        model = FuelPrices
        fields = '__all__'


class FuelPriceViewSet(viewsets.ModelViewSet):
    queryset = FuelPrices.objects.all()
    serializer_class = FuelPriceSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        if self.request.user.position == 'Cashier' or self.request.user.position == 'Manager':
            fuels = FuelPrices.objects.all()
            qs = fuels.filter(Q(gas_station_assigned=self.request.user.gas_station_assigned))
            return qs
        elif self.request.user.position == 'Owner':
            fuels = FuelPrices.objects.all()
            return fuels

    def perform_create(self, serializer):
        cashier = self.request.user.position == 'Cashier'
        manager = self.request.user.position == 'Manager'
        owner = self.request.user.position == 'Owner'

        if cashier:
            return serializer.save(created_by=self.request.user.full_name)
        elif manager:
            return serializer.save(created_by=self.request.user.full_name)
        elif owner:
            return serializer.save(created_by=self.request.user.full_name)

    def perform_update(self, serializer):
        cashier = self.request.user.position == 'Cashier'
        manager = self.request.user.position == 'Manager'
        owner = self.request.user.position == 'Owner'

        if cashier:
            return serializer.save(updated_by=self.request.user.full_name)
        elif manager:
            return serializer.save(updated_by=self.request.user.full_name)
        elif owner:
            return serializer.save(updated_by=self.request.user.full_name)