from rest_framework import serializers
from .models import InfluencerProfile, MediaPost
from accounts.serializers import UserSerializer


class MediaPostSerializer(serializers.ModelSerializer):
    """
    Serializer for MediaPost model.
    """
    engagement_rate = serializers.ReadOnlyField()
    
    class Meta:
        model = MediaPost
        fields = [
            'id',
            'media_id',
            'media_type',
            'caption',
            'timestamp',
            'impressions',
            'reach',
            'engagement',
            'saved',
            'video_views',
            'engagement_rate',
            'fetched_at'
        ]
        read_only_fields = ['id', 'fetched_at']


class InfluencerProfileSerializer(serializers.ModelSerializer):
    """
    Serializer for InfluencerProfile model.
    
    IMPORTANT: This serializer NEVER exposes the access_token field.
    """
    user = UserSerializer(read_only=True)
    is_verified = serializers.ReadOnlyField()
    data_is_fresh = serializers.ReadOnlyField()
    
    class Meta:
        model = InfluencerProfile
        fields = [
            'id',
            'user',
            'instagram_handle',
            'instagram_business_id',
            'verification_status',
            'is_verified',
            'data_is_fresh',
            # Basic metrics
            'followers_count',
            'media_count',
            # Engagement averages
            'ave_impressions',
            'ave_reach',
            'ave_engagement',
            'ave_saves',
            'ave_video_views',
            'engagement_rate_percent',
            # Growth metrics
            'follower_growth',
            'follower_growth_rate_percent',
            'profile_views',
            # Audience demographics
            'female_audience_percentage',
            'male_audience_percentage',
            'audience_summary_string',
            'audience_age_distribution',
            'audience_gender_distribution',
            'audience_top_countries',
            'audience_top_cities',
            # AI predictions (tier/popularity labels removed for user clarity)
            'ai_predicted_niche',
            'niche_probabilities',
            # Explainability
            'niche_classification_explanation',
            'authenticity_score',
            'bot_detection_flags',
            # Pricing
            'estimated_price_min',
            'estimated_price_max',
            'estimated_price_avg',
            # Metadata
            'last_data_fetch',
            'data_processing_status',
            'error_message',
            'created_at',
            'updated_at'
        ]
        read_only_fields = [
            'id',
            'instagram_business_id',
            'verification_status',
            'followers_count',
            'media_count',
            'ave_impressions',
            'ave_reach',
            'ave_engagement',
            'ave_saves',
            'ave_video_views',
            'engagement_rate_percent',
            'follower_growth',
            'follower_growth_rate_percent',
            'profile_views',
            'female_audience_percentage',
            'male_audience_percentage',
            'audience_summary_string',
            'audience_age_distribution',
            'audience_gender_distribution',
            'audience_top_countries',
            'audience_top_cities',
            'ai_predicted_niche',
            'niche_probabilities',
            'niche_classification_explanation',
            'authenticity_score',
            'bot_detection_flags',
            'estimated_price_min',
            'estimated_price_max',
            'estimated_price_avg',
            'last_data_fetch',
            'data_processing_status',
            'error_message',
            'created_at',
            'updated_at'
        ]
        # CRITICAL: Never expose access_token
        extra_kwargs = {
            'access_token': {'write_only': True}
        }


class InfluencerProfileListSerializer(serializers.ModelSerializer):
    """
    Simplified serializer for listing influencers (for business dashboard).
    Identity is hidden — businesses see stats only, not names or handles.
    """
    display_name = serializers.SerializerMethodField()

    class Meta:
        model = InfluencerProfile
        fields = [
            'id',
            'display_name',
            'followers_count',
            'engagement_rate_percent',
            'ai_predicted_niche',
            'niche_classification_explanation',
            'authenticity_score',
            'estimated_price_min',
            'estimated_price_max',
            'estimated_price_avg',
            'female_audience_percentage',
            'male_audience_percentage',
            'audience_summary_string',
            'audience_age_distribution',
            'audience_top_countries',
            'audience_top_cities',
            'ave_impressions',
            'ave_reach',
            'ave_engagement',
            'ave_saves',
            'media_count',
            'verification_status'
        ]

    def get_display_name(self, obj):
        return f"Creator #{obj.id}"







