"""
Statistics, health check, and miscellaneous domain serializers for ThrillWiki API v1.

This module contains serializers for statistics, health checks, and other
miscellaneous functionality.
"""

from drf_spectacular.utils import (
    extend_schema_field,
)
from rest_framework import serializers

from apps.core.choices.serializers import RichChoiceFieldSerializer

# === STATISTICS SERIALIZERS ===


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

    total_parks = serializers.IntegerField()
    operating_parks = serializers.IntegerField()
    closed_parks = serializers.IntegerField()
    under_construction = serializers.IntegerField()

    # Averages
    average_rating = serializers.DecimalField(max_digits=3, decimal_places=2, allow_null=True)
    average_coaster_count = serializers.DecimalField(max_digits=5, decimal_places=2, allow_null=True)

    # Top countries
    top_countries = serializers.ListField(child=serializers.DictField())

    # Recently added
    recently_added_count = serializers.IntegerField()


class RideStatsOutputSerializer(serializers.Serializer):
    """Output serializer for ride statistics."""

    total_rides = serializers.IntegerField()
    operating_rides = serializers.IntegerField()
    closed_rides = serializers.IntegerField()
    under_construction = serializers.IntegerField()

    # By category
    rides_by_category = serializers.DictField()

    # Averages
    average_rating = serializers.DecimalField(max_digits=3, decimal_places=2, allow_null=True)
    average_capacity = serializers.DecimalField(max_digits=8, decimal_places=2, allow_null=True)

    # Top manufacturers
    top_manufacturers = serializers.ListField(child=serializers.DictField())

    # Recently added
    recently_added_count = serializers.IntegerField()


class ParkReviewOutputSerializer(serializers.Serializer):
    """Output serializer for park reviews."""

    id = serializers.IntegerField()
    rating = serializers.IntegerField()
    title = serializers.CharField()
    content = serializers.CharField()
    visit_date = serializers.DateField()
    created_at = serializers.DateTimeField()

    # User info (limited for privacy)
    user = serializers.SerializerMethodField()

    @extend_schema_field(serializers.DictField())
    def get_user(self, obj) -> dict:
        return {
            "username": obj.user.username,
            "display_name": obj.user.get_full_name() or obj.user.username,
        }


# === HEALTH CHECK SERIALIZERS ===


class HealthCheckOutputSerializer(serializers.Serializer):
    """Output serializer for health check responses."""

    status = RichChoiceFieldSerializer(choice_group="health_statuses", domain="core")
    timestamp = serializers.DateTimeField()
    version = serializers.CharField()
    environment = serializers.CharField()
    response_time_ms = serializers.FloatField()
    checks = serializers.DictField()
    metrics = serializers.DictField()


class PerformanceMetricsOutputSerializer(serializers.Serializer):
    """Output serializer for performance metrics."""

    timestamp = serializers.DateTimeField()
    database_analysis = serializers.DictField()
    cache_performance = serializers.DictField()
    recent_slow_queries = serializers.ListField()


class SimpleHealthOutputSerializer(serializers.Serializer):
    """Output serializer for simple health check."""

    status = RichChoiceFieldSerializer(choice_group="simple_health_statuses", domain="core")
    timestamp = serializers.DateTimeField()
    error = serializers.CharField(required=False)
