from rest_framework import viewsets, permissions, status
from rest_framework.decorators import action
from rest_framework.response import Response
from django.conf import settings
from .models import CustomUser
from rest_framework import serializers
from .models import CustomUser

class UserMeSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomUser
        fields = ['id', 'email', 'first_name', 'last_name', 'is_serviser']

class UserViewSet(viewsets.ViewSet):
    # Akcija 'me' bit će dostupna na /api/users/me/
    @action(detail=False, methods=['get'])
    def me(self, request):
        # 1. Provjera je li korisnik stvarno logiran (ima validan token)
        if not request.user.is_authenticated:
            return Response(
                {"detail": "Niste prijavljeni. U DEBUG modu pristup je dozvoljen, ali nema podataka za AnonymousUser."},
                status=status.HTTP_200_OK # Možeš vratiti 200 s praznim podacima ili 401
            )

        # 2. Ako je logiran, normalno serijaliziraj
        serializer = UserMeSerializer(request.user)
        return Response(serializer.data)