from rest_framework import serializers
from django.contrib.auth import get_user_model
from .models import Skill, Event, TaskArea, Shift, ShiftSignup, EventTemplate
from apps.users.serializers import UserSerializer

User = get_user_model()

class SkillSerializer(serializers.ModelSerializer):
    class Meta:
        model = Skill
        fields = ['id', 'name', 'description', 'color', 'created_at']

class ShiftSignupSerializer(serializers.ModelSerializer):
    display_name = serializers.SerializerMethodField()
    is_guest = serializers.SerializerMethodField()

    class Meta:
        model = ShiftSignup
        fields = ['id', 'display_name', 'is_guest', 'created_at']

    def get_is_guest(self, obj):
        return obj.user is None

    def get_display_name(self, obj):
        request = self.context.get('request')
        # PRIVACY RULE: Guests (unauthenticated users) MUST NOT see who signed up where
        if request and not request.user.is_authenticated:
            return "Belegt (Anonym)"
        
        if obj.user:
            return obj.user.get_display_name()
        return obj.guest_name or "Gast"

class ShiftSerializer(serializers.ModelSerializer):
    required_skills = SkillSerializer(many=True, read_only=True)
    required_skill_ids = serializers.PrimaryKeyRelatedField(
        queryset=Skill.objects.all(), many=True, write_only=True, required=False, source='required_skills'
    )
    signups = serializers.SerializerMethodField()
    signups_count = serializers.SerializerMethodField()
    is_full = serializers.SerializerMethodField()

    class Meta:
        model = Shift
        fields = [
            'id', 'task_area', 'title', 'date', 'start_time', 'end_time',
            'max_participants', 'required_skills', 'required_skill_ids',
            'signups', 'signups_count', 'is_full'
        ]

    def get_signups(self, obj):
        signups_qs = obj.signups.all()
        serializer = ShiftSignupSerializer(signups_qs, many=True, context=self.context)
        return serializer.data

    def get_signups_count(self, obj):
        return obj.signups.count()

    def get_is_full(self, obj):
        return obj.signups.count() >= obj.max_participants

class TaskAreaSerializer(serializers.ModelSerializer):
    shifts = ShiftSerializer(many=True, read_only=True)

    class Meta:
        model = TaskArea
        fields = ['id', 'event', 'name', 'description', 'order', 'shifts']

class EventSerializer(serializers.ModelSerializer):
    task_areas = TaskAreaSerializer(many=True, read_only=True)
    created_by_name = serializers.SerializerMethodField()
    co_managers = UserSerializer(many=True, read_only=True)
    co_manager_ids = serializers.PrimaryKeyRelatedField(
        queryset=User.objects.all(), many=True, write_only=True, required=False, source='co_managers'
    )

    class Meta:
        model = Event
        fields = [
            'id', 'title', 'description', 'location',
            'start_date', 'end_date', 'is_active', 'created_by', 'created_by_name',
            'co_managers', 'co_manager_ids',
            'task_areas', 'created_at', 'updated_at'
        ]
        read_only_fields = ['created_by', 'created_at', 'updated_at']

    def get_created_by_name(self, obj):
        return obj.created_by.get_display_name() if obj.created_by else "Unbekannt"

class EventTemplateSerializer(serializers.ModelSerializer):
    created_by_name = serializers.SerializerMethodField()

    class Meta:
        model = EventTemplate
        fields = ['id', 'name', 'description', 'template_data', 'created_by', 'created_by_name', 'created_at']
        read_only_fields = ['created_by', 'created_at']

    def get_created_by_name(self, obj):
        return obj.created_by.get_display_name() if obj.created_by else "System"
