from rest_framework import serializers
from core.models.Subscription import Subscription
 
 

class SubscriptionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Subscription
        fields = ['user', 'license_number', 'user_count', 'startTime', 'endTime', 'price']
        read_only_fields = ['user']  # user will be set automatically by the view

    def create(self, validated_data):
        """
        Override the create method to ensure the user is assigned automatically.
        This is important since 'user' is not part of the data coming from the client
        but is assigned based on the authenticated user.
        """
        # The user is automatically added by the view (request.user)
        user = validated_data.pop('user', None)  # We do not expect user to be passed from the client
        subscription = Subscription.objects.create(**validated_data, user=user)
        return subscription
