from django.utils import timezone

from .models import User


class UserExportService:
    """Service for exporting all user data."""

    @staticmethod
    def export_user_data(user: User) -> dict:
        """
        Export all data associated with a user or an object containing counts/metadata and actual data.

        Args:
            user: The user to export data for

        Returns:
            dict: The complete user data export
        """
        # Import models locally to avoid circular imports
        from apps.lists.models import UserList
        from apps.parks.models import ParkReview
        from apps.rides.models import RideReview

        # User account and profile
        user_data = {
            "username": user.username,
            "email": user.email,
            "date_joined": user.date_joined,
            "first_name": user.first_name,
            "last_name": user.last_name,
            "is_active": user.is_active,
            "role": user.role,
        }

        profile_data = {}
        if hasattr(user, "profile"):
            profile = user.profile
            profile_data = {
                "display_name": profile.display_name,
                "bio": profile.bio,
                "location": profile.location,
                "pronouns": profile.pronouns,
                "unit_system": profile.unit_system,
                "social_media": {
                    "twitter": profile.twitter,
                    "instagram": profile.instagram,
                    "youtube": profile.youtube,
                    "discord": profile.discord,
                },
                "ride_credits": {
                    "coaster": profile.coaster_credits,
                    "dark_ride": profile.dark_ride_credits,
                    "flat_ride": profile.flat_ride_credits,
                    "water_ride": profile.water_ride_credits,
                },
            }

        # Reviews
        park_reviews = list(
            ParkReview.objects.filter(user=user).values(
                "park__name", "rating", "review", "created_at", "updated_at", "is_published"
            )
        )

        ride_reviews = list(
            RideReview.objects.filter(user=user).values(
                "ride__name", "rating", "review", "created_at", "updated_at", "is_published"
            )
        )

        # Lists
        user_lists = []
        for user_list in UserList.objects.filter(user=user):
            items = list(user_list.items.values("order", "content_type__model", "object_id", "comment"))
            user_lists.append(
                {
                    "title": user_list.title,
                    "description": user_list.description,
                    "created_at": user_list.created_at,
                    "items": items,
                }
            )

        export_data = {
            "account": user_data,
            "profile": profile_data,
            "preferences": getattr(user, "notification_preferences", {}),
            "content": {
                "park_reviews": park_reviews,
                "ride_reviews": ride_reviews,
                "lists": user_lists,
            },
            "export_info": {"generated_at": timezone.now(), "version": "1.0"},
        }

        return export_data
