from django.contrib import admin
from .models import InfluencerProfile, MediaPost


@admin.register(InfluencerProfile)
class InfluencerProfileAdmin(admin.ModelAdmin):
    """
    Admin interface for Influencer Profiles.
    """
    list_display = [
        'instagram_handle',
        'user',
        'followers_count',
        'engagement_rate_percent',
        'ai_predicted_niche',
        'verification_status',
        'data_processing_status',
        'last_data_fetch'
    ]
    
    list_filter = [
        'verification_status',
        'data_processing_status',
        'ai_predicted_niche',
        'created_at'
    ]
    
    search_fields = [
        'instagram_handle',
        'user__username',
        'user__email',
        'ai_predicted_niche'
    ]
    
    readonly_fields = [
        'created_at',
        'updated_at',
        'last_data_fetch',
        'instagram_business_id'
    ]
    
    fieldsets = (
        ('User Information', {
            'fields': ('user', 'instagram_handle', 'verification_status')
        }),
        ('Instagram Account', {
            'fields': ('instagram_business_id', 'access_token')
        }),
        ('Basic Metrics', {
            'fields': ('followers_count', 'media_count')
        }),
        ('Engagement Averages', {
            'fields': (
                'ave_impressions',
                'ave_reach',
                'ave_engagement',
                'ave_saves',
                'ave_video_views',
                'engagement_rate_percent'
            )
        }),
        ('Growth Metrics', {
            'fields': ('follower_growth', 'follower_growth_rate_percent', 'profile_views')
        }),
        ('Audience Demographics', {
            'fields': (
                'female_audience_percentage',
                'male_audience_percentage',
                'audience_summary_string',
                'audience_age_distribution',
                'audience_gender_distribution',
                'audience_top_countries',
                'audience_top_cities',
            )
        }),
        ('AI Prediction', {
            'fields': ('ai_predicted_niche',)
        }),
        ('Data Processing', {
            'fields': (
                'data_processing_status',
                'error_message',
                'last_data_fetch'
            )
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    def get_readonly_fields(self, request, obj=None):
        """Make access_token read-only to prevent accidental exposure"""
        if obj:
            return self.readonly_fields + ['access_token']
        return self.readonly_fields


@admin.register(MediaPost)
class MediaPostAdmin(admin.ModelAdmin):
    """
    Admin interface for Media Posts.
    """
    list_display = [
        'media_id',
        'influencer',
        'media_type',
        'impressions',
        'reach',
        'engagement',
        'timestamp',
        'fetched_at'
    ]
    
    list_filter = [
        'media_type',
        'timestamp',
        'fetched_at'
    ]
    
    search_fields = [
        'media_id',
        'influencer__instagram_handle',
        'caption'
    ]
    
    readonly_fields = ['fetched_at']
    
    fieldsets = (
        ('Post Information', {
            'fields': ('influencer', 'media_id', 'media_type', 'caption', 'timestamp')
        }),
        ('Metrics', {
            'fields': ('impressions', 'reach', 'engagement', 'saved', 'video_views')
        }),
        ('Metadata', {
            'fields': ('fetched_at',)
        }),
    )
