from rest_framework import serializers

from apps.accounts.serializers import UserSerializer

from .models import Review


class ReviewSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)

    class Meta:
        model = Review
        fields = [
            "id",
            "user",
            "content_type",
            "object_id",
            "rating",
            "text",
            "is_public",
            "helpful_votes",
            "created_at",
            "updated_at",
        ]
        read_only_fields = ["id", "user", "helpful_votes", "created_at", "updated_at"]

    def validate(self, data):
        """
        Check that rating is between 1 and 5.
        """
        # Rating is already validated by model field validation but explicit check is good
        return data
