"""
Social Provider Management Serializers

Serializers for handling social provider connection/disconnection requests
and responses in the ThrillWiki API.
"""

from django.contrib.auth import get_user_model
from rest_framework import serializers

User = get_user_model()


class ConnectedProviderSerializer(serializers.Serializer):
    """Serializer for connected social provider information."""

    provider = serializers.CharField(help_text="Provider ID (e.g., 'google', 'discord')")
    provider_name = serializers.CharField(help_text="Human-readable provider name")
    uid = serializers.CharField(help_text="User ID on the social provider")
    date_joined = serializers.DateTimeField(help_text="When this provider was connected")
    can_disconnect = serializers.BooleanField(help_text="Whether this provider can be safely disconnected")
    disconnect_reason = serializers.CharField(
        allow_null=True, required=False, help_text="Reason why provider cannot be disconnected (if applicable)"
    )
    extra_data = serializers.JSONField(required=False, help_text="Additional data from the social provider")


class AvailableProviderSerializer(serializers.Serializer):
    """Serializer for available social provider information."""

    id = serializers.CharField(help_text="Provider ID (e.g., 'google', 'discord')")
    name = serializers.CharField(help_text="Human-readable provider name")
    auth_url = serializers.URLField(help_text="URL to initiate authentication with this provider")
    connect_url = serializers.URLField(help_text="API URL to connect this provider")


class SocialAuthStatusSerializer(serializers.Serializer):
    """Serializer for comprehensive social authentication status."""

    user_id = serializers.IntegerField(help_text="User's ID")
    username = serializers.CharField(help_text="User's username")
    email = serializers.EmailField(help_text="User's email address")
    has_password_auth = serializers.BooleanField(help_text="Whether user has email/password authentication set up")
    connected_providers = ConnectedProviderSerializer(many=True, help_text="List of connected social providers")
    total_auth_methods = serializers.IntegerField(help_text="Total number of authentication methods available")
    can_disconnect_any = serializers.BooleanField(help_text="Whether user can safely disconnect any provider")
    requires_password_setup = serializers.BooleanField(
        help_text="Whether user needs to set up password before disconnecting"
    )


class ConnectProviderInputSerializer(serializers.Serializer):
    """Serializer for social provider connection requests."""

    provider = serializers.CharField(help_text="Provider ID to connect (e.g., 'google', 'discord')")

    def validate_provider(self, value):
        """Validate that the provider is supported and configured."""
        from apps.accounts.services.social_provider_service import SocialProviderService

        is_valid, message = SocialProviderService.validate_provider_exists(value)
        if not is_valid:
            raise serializers.ValidationError(message)

        return value


class ConnectProviderOutputSerializer(serializers.Serializer):
    """Serializer for social provider connection responses."""

    success = serializers.BooleanField(help_text="Whether the connection was successful")
    message = serializers.CharField(help_text="Success or error message")
    provider = serializers.CharField(help_text="Provider that was connected")
    auth_url = serializers.URLField(required=False, help_text="URL to complete the connection process")


class DisconnectProviderOutputSerializer(serializers.Serializer):
    """Serializer for social provider disconnection responses."""

    success = serializers.BooleanField(help_text="Whether the disconnection was successful")
    message = serializers.CharField(help_text="Success or error message")
    provider = serializers.CharField(help_text="Provider that was disconnected")
    remaining_providers = serializers.ListField(
        child=serializers.CharField(), help_text="List of remaining connected providers"
    )
    has_password_auth = serializers.BooleanField(help_text="Whether user still has password authentication")
    suggestions = serializers.ListField(
        child=serializers.CharField(),
        required=False,
        help_text="Suggestions for maintaining account access (if applicable)",
    )


class SocialProviderListOutputSerializer(serializers.Serializer):
    """Serializer for listing available social providers."""

    available_providers = AvailableProviderSerializer(many=True, help_text="List of available social providers")
    count = serializers.IntegerField(help_text="Number of available providers")


class ConnectedProvidersListOutputSerializer(serializers.Serializer):
    """Serializer for listing connected social providers."""

    connected_providers = ConnectedProviderSerializer(many=True, help_text="List of connected social providers")
    count = serializers.IntegerField(help_text="Number of connected providers")
    has_password_auth = serializers.BooleanField(help_text="Whether user has password authentication")
    can_disconnect_any = serializers.BooleanField(help_text="Whether user can safely disconnect any provider")


class SocialProviderErrorSerializer(serializers.Serializer):
    """Serializer for social provider error responses."""

    error = serializers.CharField(help_text="Error message")
    code = serializers.CharField(required=False, help_text="Error code for programmatic handling")
    suggestions = serializers.ListField(
        child=serializers.CharField(), required=False, help_text="Suggestions for resolving the error"
    )
    provider = serializers.CharField(required=False, help_text="Provider related to the error (if applicable)")
