"""
Serializers for park review API endpoints.

This module contains serializers for park review CRUD operations.
"""

from drf_spectacular.utils import OpenApiExample, extend_schema_field, extend_schema_serializer
from rest_framework import serializers

from apps.api.v1.serializers.reviews import ReviewUserSerializer
from apps.parks.models.reviews import ParkReview


@extend_schema_serializer(
    examples=[
        OpenApiExample(
            name="Complete Park Review",
            summary="Full park review response",
            description="Example response showing all fields for a park review",
            value={
                "id": 123,
                "title": "Great family park!",
                "content": "We had a wonderful time. The atmosphere is charming.",
                "rating": 9,
                "visit_date": "2023-06-15",
                "created_at": "2023-06-16T10:30:00Z",
                "updated_at": "2023-06-16T10:30:00Z",
                "is_published": True,
                "user": {
                    "username": "park_fan",
                    "display_name": "Park Fan",
                    "avatar_url": "https://example.com/avatar.jpg",
                },
                "park": {"id": 101, "name": "Cedar Point", "slug": "cedar-point"},
            },
        )
    ]
)
class ParkReviewOutputSerializer(serializers.ModelSerializer):
    """Output serializer for park reviews."""

    user = ReviewUserSerializer(read_only=True)
    park = serializers.SerializerMethodField()

    class Meta:
        model = ParkReview
        fields = [
            "id",
            "title",
            "content",
            "rating",
            "visit_date",
            "created_at",
            "updated_at",
            "is_published",
            "user",
            "park",
        ]
        read_only_fields = [
            "id",
            "created_at",
            "updated_at",
            "user",
            "park",
        ]

    @extend_schema_field(serializers.DictField())
    def get_park(self, obj):
        """Get park information."""
        return {
            "id": obj.park.id,
            "name": obj.park.name,
            "slug": obj.park.slug,
        }


class ParkReviewCreateInputSerializer(serializers.ModelSerializer):
    """Input serializer for creating park reviews."""

    class Meta:
        model = ParkReview
        fields = [
            "title",
            "content",
            "rating",
            "visit_date",
        ]

    def validate_rating(self, value):
        """Validate rating is between 1 and 10."""
        if not (1 <= value <= 10):
            raise serializers.ValidationError("Rating must be between 1 and 10.")
        return value


class ParkReviewUpdateInputSerializer(serializers.ModelSerializer):
    """Input serializer for updating park reviews."""

    class Meta:
        model = ParkReview
        fields = [
            "title",
            "content",
            "rating",
            "visit_date",
        ]

    def validate_rating(self, value):
        """Validate rating is between 1 and 10."""
        if not (1 <= value <= 10):
            raise serializers.ValidationError("Rating must be between 1 and 10.")
        return value


class ParkReviewListOutputSerializer(serializers.ModelSerializer):
    """Simplified output serializer for park review lists."""

    user = ReviewUserSerializer(read_only=True)
    park_name = serializers.CharField(source="park.name", read_only=True)

    class Meta:
        model = ParkReview
        fields = [
            "id",
            "title",
            "rating",
            "visit_date",
            "created_at",
            "is_published",
            "user",
            "park_name",
        ]
        read_only_fields = fields


class ParkReviewStatsOutputSerializer(serializers.Serializer):
    """Output serializer for park review statistics."""

    total_reviews = serializers.IntegerField()
    published_reviews = serializers.IntegerField()
    pending_reviews = serializers.IntegerField()
    average_rating = serializers.FloatField(allow_null=True)
    rating_distribution = serializers.DictField(
        child=serializers.IntegerField(), help_text="Count of reviews by rating (1-10)"
    )
    recent_reviews = serializers.IntegerField()


class ParkReviewModerationInputSerializer(serializers.Serializer):
    """Input serializer for review moderation operations."""

    review_ids = serializers.ListField(child=serializers.IntegerField(), help_text="List of review IDs to moderate")
    action = serializers.ChoiceField(
        choices=[
            ("publish", "Publish"),
            ("unpublish", "Unpublish"),
            ("delete", "Delete"),
        ],
        help_text="Moderation action to perform",
    )
    moderation_notes = serializers.CharField(
        required=False, allow_blank=True, help_text="Optional notes about the moderation action"
    )
