"""
Django Admin Configuration for Agency and Brand Management

This module provides comprehensive admin interface configurations for:
- Agency: Marketing/advertising agencies
- Brand: Brands managed by agencies  
- Advertisers: Users associated with brands

Author: Generated for Django Admin Interface
Created: 2025
"""

from django.contrib import admin
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.db.models import Count, Q
from django.contrib import messages
from django.contrib.admin import SimpleListFilter

from .models import Agency, Brand, Advertiser, BrandCategory, BrandStatus, AgencyType, AgencyTeamMember


# ============================================================================
# CUSTOM FILTERS FOR ENHANCED ADMIN FUNCTIONALITY
# ============================================================================

class BrandCountFilter(SimpleListFilter):
    """
    Custom filter to filter agencies by number of brands they manage.
    Provides quick filtering options for agencies with different brand volumes.
    """
    title = _('Brand Count')
    parameter_name = 'brand_count'

    def lookups(self, request, model_admin):
        """Define filter options for brand count ranges"""
        return (
            ('0', _('No Brands')),
            ('1-5', _('1-5 Brands')),
            ('6-10', _('6-10 Brands')),
            ('11+', _('11+ Brands')),
        )

    def queryset(self, request, queryset):
        """Apply filtering logic based on selected option"""
        queryset = queryset.annotate(brand_count=Count('brands'))
        
        if self.value() == '0':
            return queryset.filter(brand_count=0)
        elif self.value() == '1-5':
            return queryset.filter(brand_count__range=(1, 5))
        elif self.value() == '6-10':
            return queryset.filter(brand_count__range=(6, 10))
        elif self.value() == '11+':
            return queryset.filter(brand_count__gte=11)
        
        return queryset


class ActiveBrandsFilter(SimpleListFilter):
    """
    Filter to show agencies based on their active brand status.
    Helps identify agencies with active vs inactive brand portfolios.
    """
    title = _('Active Brands Status')
    parameter_name = 'active_brands'

    def lookups(self, request, model_admin):
        """Define filter options for active brand status"""
        return (
            ('has_active', _('Has Active Brands')),
            ('no_active', _('No Active Brands')),
            ('all_active', _('All Brands Active')),
        )

    def queryset(self, request, queryset):
        """Apply filtering logic for active brands"""
        if self.value() == 'has_active':
            return queryset.filter(brands__status=BrandStatus.ACTIVE).distinct()
        elif self.value() == 'no_active':
            return queryset.exclude(brands__status=BrandStatus.ACTIVE).distinct()
        elif self.value() == 'all_active':
            # Agencies where all brands are active (excluding agencies with no brands)
            return queryset.annotate(
                total_brands=Count('brands'),
                active_brands=Count('brands', filter=Q(brands__status=BrandStatus.ACTIVE))
            ).filter(
                total_brands__gt=0,
                total_brands=models.F('active_brands')
            )
        
        return queryset


# ============================================================================
# INLINE ADMIN CLASSES FOR RELATED OBJECTS
# ============================================================================

class BrandInline(admin.TabularInline):
    """
    Inline admin for Brand model within Agency admin.
    Allows management of brands directly from the agency detail page.
    """
    model = Brand
    extra = 1  # Number of empty forms to display
    min_num = 0  # Minimum number of forms required
    max_num = 10  # Maximum number of inline forms allowed
    
    # Fields to display in the inline form
    fields = ('name', 'category', 'status', 'is_featured', 'logo_preview')
    readonly_fields = ('logo_preview',)  # Make logo preview read-only
    
    # Customize inline form appearance
    classes = ('collapse',)  # Make inline collapsible
    verbose_name = _('Brand')
    verbose_name_plural = _('Brands')

    def logo_preview(self, obj):
        """
        Display a small preview of the brand logo in the inline form.
        Returns HTML for image display or placeholder text.
        """
        if obj.logo:
            return format_html(
                '<img src="{}" width="50" height="50" style="object-fit: cover; border-radius: 4px;" />',
                obj.logo.url
            )
        return _('No logo')
    
    logo_preview.short_description = _('Logo Preview')


class AdvertisersInline(admin.TabularInline):
    """
    Inline admin for Advertisers model within Brand admin.
    Shows users associated with each brand for quick management.
    """
    model = Advertiser
    extra = 1  # Number of empty forms to display
    min_num = 0  # Minimum number of forms required
    
    # Fields to display in the inline form
    fields = ('id_user', 'status')
    
    # Customize appearance
    verbose_name = _('Advertiser')
    verbose_name_plural = _('Advertisers')


# ============================================================================
# MAIN ADMIN CLASSES
# ============================================================================

@admin.register(Agency)
class AgencyAdmin(admin.ModelAdmin):
    """
    Comprehensive admin interface for Agency model.
    Provides full CRUD operations with enhanced filtering and display options.
    """
    
    # ===== LIST VIEW CONFIGURATION =====
    list_display = (
        'name',                    # Agency name
        'owner_link',             # Clickable link to owner profile
        'brand_count',            # Number of brands managed
        'active_brand_count',     # Number of active brands
        'description_short',      # Truncated description
        'created_at',           # Creation date
        'status_indicator',       # Visual status indicator
    )
    
    # Fields that can be clicked to view/edit the object
    list_display_links = ('name',)
    
    # Fields available for filtering in the right sidebar
    list_filter = (
        BrandCountFilter,         # Custom filter for brand count
        ActiveBrandsFilter,       # Custom filter for active brands 
        'is_deleted',             # Filter by deletion status
        'owner',                  # Filter by owner
    )
    
    # Fields available for search functionality
    search_fields = (
        'name',                   # Search by agency name
        'description',            # Search by description
        'owner__username',        # Search by owner username
        'owner__email',           # Search by owner email
    )
    
    # Default ordering of the list view
    ordering = ('name',)
    
    # Number of items per page
    list_per_page = 25
    
    # Enable select all checkbox
    list_select_related = ('owner',)  # Optimize database queries
    
    # ===== DETAIL VIEW CONFIGURATION =====
    fieldsets = (
        (_('Basic Information'), {
            'fields': ('name', 'owner', 'description'),
            'description': _('Core agency information and ownership details.'),
        }),
        (_('Metadata'), {
            'fields': ( 'is_deleted', 'created_at'),
            'classes': ('collapse',),  # Collapsible section
            'description': _('Administrative metadata and status information.'),
        }),
    )
    
    # Related object inlines
    inlines = [BrandInline]
    
    # ===== CUSTOM ADMIN METHODS =====
    
    def owner_link(self, obj):
        """
        Create a clickable link to the owner's user profile.
        Provides easy navigation to related user information.
        """
        if obj.owner:
            url = reverse('admin:accounts_user_change', args=[obj.owner.pk])
            return format_html('<a href="{}">{}</a>', url, obj.owner.username)
        return _('No Owner')
    
    owner_link.short_description = _('Owner')
    owner_link.admin_order_field = 'owner__username'  # Enable sorting
    
    def brand_count(self, obj):
        """
        Display the total number of brands managed by this agency.
        Includes a link to filter brands by this agency.
        """
        count = obj.brands.count()
        if count > 0:
            url = reverse('admin:agencies_brand_changelist')
            return format_html(
                '<a href="{}?agency__id__exact={}">{} brands</a>',
                url, obj.pk, count
            )
        return '0 brands'
    
    brand_count.short_description = _('Total Brands')
    
    def active_brand_count(self, obj):
        """
        Display the number of active brands managed by this agency.
        Provides quick insight into agency activity level.
        """
        count = obj.brands.filter(status=BrandStatus.ACTIVE).count()
        return f'{count} active'
    
    active_brand_count.short_description = _('Active Brands')
    
    def description_short(self, obj):
        """
        Display truncated description for better list view readability.
        Shows first 50 characters with ellipsis if longer.
        """
        if obj.description:
            return (obj.description[:50] + '...') if len(obj.description) > 50 else obj.description
        return _('No description')
    
    description_short.short_description = _('Description')
    
    def status_indicator(self, obj):
        """
        Visual indicator for agency status based on deletion flag.
        Uses color-coded badges for quick status recognition.
        """
        if obj.is_deleted:
            return format_html(
                '<span style="color: red; font-weight: bold;">●</span> {}',
                _('Deleted')
            )
        else:
            return format_html(
                '<span style="color: green; font-weight: bold;">●</span> {}',
                _('Active')
            )
    
    status_indicator.short_description = _('Status')
    
    # ===== ADMIN ACTIONS =====
    
    def mark_as_deleted(self, request, queryset):
        """
        Bulk action to mark selected agencies as deleted.
        Soft delete functionality for data preservation.
        """
        updated = queryset.update(is_deleted=1)
        self.message_user(
            request,
            f'{updated} agencies were marked as deleted.',
        )
    
    mark_as_deleted.short_description = _('Mark selected agencies as deleted')
    
    def restore_agencies(self, request, queryset):
        """
        Bulk action to restore deleted agencies.
        Allows recovery of soft-deleted records.
        """
        updated = queryset.update(is_deleted=0)
        self.message_user(
            request,
            f'{updated} agencies were restored.',
        )
    
    restore_agencies.short_description = _('Restore selected agencies')
    
    # Register custom actions
    actions = ['mark_as_deleted', 'restore_agencies']


@admin.register(Brand)
class BrandAdmin(admin.ModelAdmin):
    """
    Comprehensive admin interface for Brand model.
    Provides advanced brand management with visual elements and filtering.
    """
    
    # ===== LIST VIEW CONFIGURATION =====
    list_display = (
        'logo_thumbnail',         # Small logo preview
        'name',                   # Brand name
        'agency_link',            # Link to parent agency
        'category',               # Brand category
        'status_badge',           # Visual status indicator
        'is_featured',            # Featured status
        'advertiser_count',       # Number of associated advertisers
        'created_info',           # Creation information
    )
    
    # Clickable fields in list view
    list_display_links = ('name', 'logo_thumbnail')
    
    # # Editable fields directly in list view
    # list_editable = ('status', 'is_featured')
    list_editable = ('is_featured',)
    
    # Filtering options in right sidebar
    list_filter = (
        'status',                 # Filter by brand status
        'category',               # Filter by category
        'is_featured',            # Filter by featured status
        'agency',                 # Filter by agency
        'created_at',             # Filter by creation date (assuming BaseModel has this)
    )
    
    # Search functionality
    search_fields = (
        'name',                   # Search by brand name
        'description',            # Search by description
        'agency__name',           # Search by agency name
    )
    
    # Default ordering
    ordering = ('name',)
    
    # Pagination settings
    list_per_page = 30
    
    # Optimize database queries
    list_select_related = ('agency',)
    
    # ===== DETAIL VIEW CONFIGURATION =====
    fieldsets = (
        (_('Brand Identity'), {
            'fields': ('name', 'logo', 'logo_preview', 'description'),
            'description': _('Core brand identity elements including name, logo, and description.'),
        }),
        (_('Classification'), {
            'fields': ('agency', 'category', 'status', 'is_featured'),
            'description': _('Brand categorization and status information.'),
        }),
    )
    
    # Read-only fields
    readonly_fields = ('logo_preview',)
    
    # Form widget customization
    filter_horizontal = ()  # For ManyToMany fields if any
    
    # Related object inlines
    inlines = [AdvertisersInline]
    
    # ===== CUSTOM ADMIN METHODS =====
    
    def logo_thumbnail(self, obj):
        """
        Display small logo thumbnail in list view.
        Provides visual brand identification at a glance.
        """
        if obj.logo:
            return format_html(
                '<img src="{}" width="40" height="40" '
                'style="object-fit: cover; border-radius: 4px; border: 1px solid #ddd;" />',
                obj.logo.url
            )
        return format_html(
            '<div style="width: 40px; height: 40px; background: #f0f0f0; '
            'border-radius: 4px; display: flex; align-items: center; '
            'justify-content: center; font-size: 12px; color: #666;">No Logo</div>'
        )
    
    logo_thumbnail.short_description = _('Logo')
    
    def logo_preview(self, obj):
        """
        Display larger logo preview in detail view.
        Shows full-size logo for detailed inspection.
        """
        if obj.logo:
            return format_html(
                '<img src="{}" style="max-width: 200px; max-height: 200px; '
                'object-fit: contain; border: 1px solid #ddd; border-radius: 4px;" />',
                obj.logo.url
            )
        return _('No logo uploaded')
    
    logo_preview.short_description = _('Logo Preview')
    
    def agency_link(self, obj):
        """
        Create clickable link to the parent agency.
        Enables easy navigation to agency details.
        """
        if obj.agency:
            url = reverse('admin:agencies_agency_change', args=[obj.agency.pk])
            return format_html('<a href="{}">{}</a>', url, obj.agency.name)
        return _('No Agency')
    
    agency_link.short_description = _('Agency')
    agency_link.admin_order_field = 'agency__name'  # Enable sorting
    
    def status_badge(self, obj):
        """
        Display color-coded status badge for visual status identification.
        Uses different colors for different status types.
        """
        status_colors = {
            BrandStatus.ACTIVE: '#28a745',      # Green
            BrandStatus.INACTIVE: '#6c757d',    # Gray
            BrandStatus.PENDING: '#ffc107',     # Yellow
            BrandStatus.SUSPENDED: '#dc3545',   # Red
            BrandStatus.ARCHIVED: '#17a2b8',    # Blue
        }
        
        color = status_colors.get(obj.status, '#6c757d')
        return format_html(
            '<span style="background-color: {}; color: white; padding: 3px 8px; '
            'border-radius: 12px; font-size: 11px; font-weight: bold;">{}</span>',
            color,
            obj.get_status_display()
        )
    
    status_badge.short_description = _('Status')
    status_badge.admin_order_field = 'status'  # Enable sorting
    
    def advertiser_count(self, obj):
        """
        Display count of advertisers associated with this brand.
        Provides insight into brand team size.
        """
        count = obj.advertisers_set.count()
        if count > 0:
            url = reverse('admin:agencies_advertisers_changelist')
            return format_html(
                '<a href="{}?brand__id__exact={}">{} advertisers</a>',
                url, obj.pk, count
            )
        return '0 advertisers'
    
    advertiser_count.short_description = _('Advertisers')
    
    def created_info(self, obj):
        """
        Display creation date information.
        Shows when the brand was added to the system.
        """
        if hasattr(obj, 'created_at') and obj.created_at:
            return obj.created_at.strftime('%Y-%m-%d')
        return _('Unknown')
    
    created_info.short_description = _('Created')
    
    # ===== BULK ACTIONS =====
    
    def activate_brands(self, request, queryset):
        """
        Bulk action to activate selected brands.
        Sets status to active for multiple brands at once.
        """
        updated = queryset.update(status=BrandStatus.ACTIVE)
        self.message_user(
            request,
            f'{updated} brands were activated.',
        )
    
    activate_brands.short_description = _('Activate selected brands')
    
    def feature_brands(self, request, queryset):
        """
        Bulk action to mark selected brands as featured.
        Highlights important brands across the platform.
        """
        updated = queryset.update(is_featured=True)
        self.message_user(
            request,
            f'{updated} brands were marked as featured.',
        )
    
    feature_brands.short_description = _('Mark selected brands as featured')
    
    def unfeature_brands(self, request, queryset):
        """
        Bulk action to remove featured status from selected brands.
        Removes highlighting from brands.
        """
        updated = queryset.update(is_featured=False)
        self.message_user(
            request,
            f'{updated} brands were unmarked as featured.',
        )
    
    unfeature_brands.short_description = _('Remove featured status from selected brands')
    
    # Register custom actions
    actions = ['activate_brands', 'feature_brands', 'unfeature_brands']


@admin.register(Advertiser)
class AdvertisersAdmin(admin.ModelAdmin):
    """
    Admin interface for Advertisers model.
    Manages the relationship between users and brands.
    """
    
    # ===== LIST VIEW CONFIGURATION =====
    list_display = (
        'user_info',              # User information with link
        'brand_link',             # Brand information with link
        'status_display',         # Status with formatting
        'relationship_info',      # Additional relationship context
    )
    
    # Clickable fields
    list_display_links = ('user_info',)
    
    # # Editable fields in list view
    # list_editable = ('status',)
    
    # Filtering options
    list_filter = (
        'status',                 # Filter by status
        'brand__agency',          # Filter by brand's agency
        'brand__category',        # Filter by brand category
        'brand__status',          # Filter by brand status
    )
    
    # Search functionality
    search_fields = (
        'id_user__username',      # Search by username
        'id_user__email',         # Search by email
        'id_user__first_name',    # Search by first name
        'id_user__last_name',     # Search by last name
        'brand__name',            # Search by brand name
    )
    
    # Default ordering
    ordering = ('id_user__username', 'brand__name')
    
    # Optimize queries
    list_select_related = ('id_user', 'brand', 'brand__agency')
    
    # Pagination
    list_per_page = 25
    
    # ===== DETAIL VIEW CONFIGURATION =====
    fieldsets = (
        (_('Relationship'), {
            'fields': ('id_user', 'brand', 'status'),
            'description': _('Define the relationship between user and brand.'),
        }),
    )
    
    # ===== CUSTOM ADMIN METHODS =====
    
    def user_info(self, obj):
        """
        Display comprehensive user information with link.
        Shows name, username, and email for easy identification.
        """
        if obj.id_user:
            url = reverse('admin:accounts_user_change', args=[obj.id_user.pk])
            display_name = f"{obj.id_user.get_full_name()}" if obj.id_user.get_full_name() else obj.id_user.username
            return format_html(
                '<a href="{}">{}</a><br><small style="color: #666;">{}</small>',
                url, display_name, obj.id_user.email or obj.id_user.username
            )
        return _('No User')
    
    user_info.short_description = _('User')
    user_info.admin_order_field = 'id_user__username'
    
    def brand_link(self, obj):
        """
        Display brand information with link to brand admin.
        Shows brand name and parent agency for context.
        """
        if obj.brand:
            url = reverse('admin:agencies_brand_change', args=[obj.brand.pk])
            agency_name = obj.brand.agency.name if obj.brand.agency else _('No Agency')
            return format_html(
                '<a href="{}">{}</a><br><small style="color: #666;">{}</small>',
                url, obj.brand.name, agency_name
            )
        return _('No Brand')
    
    brand_link.short_description = _('Brand')
    brand_link.admin_order_field = 'brand__name'
    
    def status_display(self, obj):
        """
        Display formatted status information.
        Provides visual styling for different status types.
        """
        if obj.status:
            # Define status colors (you can customize these)
            status_colors = {
                'active': '#28a745',
                'inactive': '#6c757d',
                'pending': '#ffc107',
                'suspended': '#dc3545',
            }
            
            # Get color based on status (default to gray if not found)
            color = status_colors.get(obj.status.lower(), '#6c757d')
            
            return format_html(
                '<span style="color: {}; font-weight: bold;">{}</span>',
                color, obj.status.title()
            )
        return _('No Status')
    
    status_display.short_description = _('Status')
    status_display.admin_order_field = 'status'
    
    def relationship_info(self, obj):
        """
        Display additional context about the user-brand relationship.
        Shows helpful metadata for relationship management.
        """
        info_parts = []
        
        # Add brand category information
        if obj.brand and obj.brand.category:
            info_parts.append(f"Category: {obj.brand.get_category_display()}")
        
        # Add brand status
        if obj.brand and obj.brand.status:
            info_parts.append(f"Brand: {obj.brand.get_status_display()}")
        
        # Add user status if available
        if obj.id_user and hasattr(obj.id_user, 'is_active'):
            user_status = "Active User" if obj.id_user.is_active else "Inactive User"
            info_parts.append(user_status)
        
        return format_html('<br>'.join(info_parts)) if info_parts else _('No additional info')
    
    relationship_info.short_description = _('Additional Info')
    
    # ===== BULK ACTIONS =====
    
    def activate_relationships(self, request, queryset):
        """
        Bulk action to activate selected advertiser relationships.
        """
        updated = queryset.update(status='active')
        self.message_user(
            request,
            f'{updated} advertiser relationships were activated.',
        )
    
    activate_relationships.short_description = _('Activate selected relationships')
    
    def deactivate_relationships(self, request, queryset):
        """
        Bulk action to deactivate selected advertiser relationships.
        """
        updated = queryset.update(status='inactive')
        self.message_user(
            request,
            f'{updated} advertiser relationships were deactivated.',
        )
    
    deactivate_relationships.short_description = _('Deactivate selected relationships')
    
    # Register actions
    actions = ['activate_relationships', 'deactivate_relationships']


# ============================================================================
# ADMIN SITE CUSTOMIZATION
# ============================================================================

# Customize admin site headers and titles
admin.site.site_header = _('Agency & Brand Management System')
admin.site.site_title = _('Agency Admin')
admin.site.index_title = _('Welcome to Agency & Brand Management')

# Optional: Register models individually if needed
# admin.site.register(Agency, AgencyAdmin)
# admin.site.register(Brand, BrandAdmin)
# admin.site.register(Advertisers, AdvertisersAdmin)

# ============================================================================
# AGENCYTYPE ADMIN CONFIGURATION
# ============================================================================

class PremiumTypeFilter(SimpleListFilter):
    """
    Filter to distinguish between premium and standard agency types.
    """
    title = _('Premium Status')
    parameter_name = 'premium_status'

    def lookups(self, request, model_admin):
        """Define filter options for premium status"""
        return (
            ('premium', _('Premium Types')),
            ('standard', _('Standard Types')),
        )

    def queryset(self, request, queryset):
        """Apply filtering logic based on premium status"""
        if self.value() == 'premium':
            return queryset.filter(is_premium=True)
        elif self.value() == 'standard':
            return queryset.filter(is_premium=False)
        return queryset


class AgencyTypeUsageFilter(SimpleListFilter):
    """
    Filter agency types by the number of agencies using each type.
    """
    title = _('Usage Count')
    parameter_name = 'agency_usage'

    def lookups(self, request, model_admin):
        """Define filter options for agency usage"""
        return (
            ('unused', _('Unused Types')),
            ('1-10', _('1-10 Agencies')),
            ('11-50', _('11-50 Agencies')),
            ('50+', _('50+ Agencies')),
        )

    def queryset(self, request, queryset):
        """Apply filtering logic based on agency count"""
        queryset = queryset.annotate(agency_count=Count('agencies', filter=Q(agencies__is_deleted=False)))
        
        if self.value() == 'unused':
            return queryset.filter(agency_count=0)
        elif self.value() == '1-10':
            return queryset.filter(agency_count__range=(1, 10))
        elif self.value() == '11-50':
            return queryset.filter(agency_count__range=(11, 50))
        elif self.value() == '50+':
            return queryset.filter(agency_count__gte=50)
        
        return queryset


@admin.register(AgencyType)
class AgencyTypeAdmin(admin.ModelAdmin):
    """
    Comprehensive admin configuration for AgencyType model.
    
    This admin class provides:
    - Enhanced list display with visual elements
    - Custom filters for better navigation
    - Bulk actions for efficient management
    - Search and ordering capabilities
    """
    
    # ===== LIST DISPLAY CONFIGURATION =====
    list_display = [
        'name_with_color',
        'description_truncated', 
        'sort_order',
        'is_premium_display',
        'agency_count_display',
        'usage_percentage',
        'created_at_formatted',
        'status_indicator'
    ]
    
    list_display_links = ['name_with_color']
    list_per_page = 25
    
    # ===== FILTERING AND SEARCH =====
    list_filter = [
        PremiumTypeFilter,
        AgencyTypeUsageFilter,
        'is_premium',
        'created_at',
        'updated_at',
    ]
    
    search_fields = [
        'name',
        'description',
    ]
    
    # ===== ORDERING =====
    ordering = ['sort_order', 'name']
    
    # ===== FIELDSET ORGANIZATION =====
    fieldsets = (
        (_('Basic Information'), {
            'fields': (
                'name',
                'description',
            ),
            'description': _('Core information about this agency type.')
        }),
        (_('Display Settings'), {
            'fields': (
                'color_code',
                'sort_order',
            ),
            'description': _('Visual appearance and ordering preferences.')
        }),
        (_('Features & Status'), {
            'fields': (
                'is_premium',
                'is_deleted',
            ),
            'description': _('Feature flags and status settings.')
        }),
        (_('Metadata'), {
            'fields': (
                'created_at',
                'updated_at',
            ),
            'classes': ('collapse',),
            'description': _('System-generated timestamps.')
        }),
    )
    
    # ===== READ-ONLY FIELDS =====
    readonly_fields = [
        'created_at',
        'updated_at',
        'color_preview',
        'detailed_agency_stats'
    ]
    
    # ===== CUSTOM DISPLAY METHODS =====
    
    def name_with_color(self, obj):
        """
        Display agency type name with color indicator.
        """
        if obj.color_code:
            return format_html(
                '<div style="display: flex; align-items: center;">'
                '<div style="width: 20px; height: 20px; background-color: {}; '
                'border-radius: 3px; margin-right: 10px; border: 1px solid #ccc;"></div>'
                '<strong>{}</strong></div>',
                obj.color_code,
                obj.name
            )
        return format_html('<strong>{}</strong>', obj.name)
    
    name_with_color.short_description = _('Agency Type')
    name_with_color.admin_order_field = 'name'
    
    def description_truncated(self, obj):
        """
        Display truncated description with tooltip.
        """
        if obj.description:
            if len(obj.description) > 80:
                truncated = obj.description[:80] + '...'
                return format_html(
                    '<span title="{}">{}</span>',
                    obj.description,
                    truncated
                )
            return obj.description
        return format_html('<em style="color: #999;">{}</em>', _('No description'))
    
    description_truncated.short_description = _('Description')
    
    def is_premium_display(self, obj):
        """
        Display premium status with visual indicator.
        """
        if obj.is_premium:
            return format_html(
                '<span style="color: #ff6b35; font-weight: bold;">'
                '⭐ {}</span>',
                _('Premium')
            )
        return format_html(
            '<span style="color: #6c757d;">{}</span>',
            _('Standard')
        )
    
    is_premium_display.short_description = _('Type')
    is_premium_display.admin_order_field = 'is_premium'
    
    def agency_count_display(self, obj):
        """
        Display count of agencies using this type with link.
        """
        count = getattr(obj, 'agency_count', None)
        if count is None:
            count = obj.agencies.filter(is_deleted=False).count()
        
        if count > 0:
            # Create link to filtered agency list
            try:
                url = reverse('admin:agencies_agency_changelist')
                return format_html(
                    '<a href="{}?agency_type__id__exact={}" style="font-weight: bold; color: #007cba;">'
                    '{} {}</a>',
                    url,
                    obj.id,
                    count,
                    _('agencies') if count != 1 else _('agency')
                )
            except:
                return format_html('<span style="font-weight: bold;">{} {}</span>', 
                                 count, _('agencies') if count != 1 else _('agency'))
        return format_html('<span style="color: #999;">0 {}</span>', _('agencies'))
    
    agency_count_display.short_description = _('Usage')
    
    def usage_percentage(self, obj):
        """
        Display usage percentage among all agencies.
        """
        try:
            total_agencies = Agency.objects.filter(is_deleted=False).count()
            if total_agencies == 0:
                return format_html('<span style="color: #999;">-</span>')
            
            count = getattr(obj, 'agency_count', None)
            if count is None:
                count = obj.agencies.filter(is_deleted=False).count()
            
            percentage = (count / total_agencies) * 100
            
            # Color code based on percentage
            if percentage >= 20:
                color = '#28a745'  # Green
            elif percentage >= 10:
                color = '#ffc107'  # Yellow
            else:
                color = '#6c757d'  # Gray
                
            return format_html(
                '<span style="color: {}; font-weight: bold;">{:.1f}%</span>',
                color,
                percentage
            )
        except Exception:
            return format_html('<span style="color: #dc3545;">-</span>')
    
    usage_percentage.short_description = _('Market Share')
    
    def created_at_formatted(self, obj):
        """
        Display formatted creation date.
        """
        if obj.created_at:
            return obj.created_at.strftime('%Y-%m-%d')
        return '-'
    
    created_at_formatted.short_description = _('Created')
    created_at_formatted.admin_order_field = 'created_at'
    
    def status_indicator(self, obj):
        """
        Display overall status indicator.
        """
        indicators = []
        
        # Premium indicator
        if obj.is_premium:
            indicators.append('<span title="Premium Type">⭐</span>')
        
        # Usage indicator
        count = getattr(obj, 'agency_count', None)
        if count is None:
            count = obj.agencies.filter(is_deleted=False).count()
            
        if count == 0:
            indicators.append('<span title="Unused Type" style="color: #dc3545;">⚠️</span>')
        elif count >= 10:
            indicators.append('<span title="Popular Type" style="color: #28a745;">📈</span>')
        
        # Soft delete indicator
        if obj.is_deleted:
            indicators.append('<span title="Deleted" style="color: #dc3545;">🗑️</span>')
        
        return format_html(' '.join(indicators)) if indicators else '-'
    
    status_indicator.short_description = _('Status')
    
    def color_preview(self, obj):
        """
        Display color preview in form (readonly field).
        """
        if obj.color_code:
            return format_html(
                '<div style="width: 60px; height: 30px; background-color: {}; '
                'border: 1px solid #ccc; border-radius: 3px; display: inline-block; '
                'margin-right: 10px; vertical-align: middle;"></div>'
                '<span style="vertical-align: middle; color: #666;">{}</span>',
                obj.color_code,
                obj.color_code
            )
        return format_html('<em>{}</em>', _('No color set'))
    
    color_preview.short_description = _('Color Preview')
    
    def detailed_agency_stats(self, obj):
        """
        Display detailed agency count information (readonly field).
        """
        if obj.pk:
            total = obj.agencies.count()
            active = obj.agencies.filter(is_active=True, is_deleted=False).count()
            inactive = obj.agencies.filter(is_active=False, is_deleted=False).count()
            deleted = obj.agencies.filter(is_deleted=True).count()
            
            return format_html(
                '<div style="font-size: 12px;">'
                '<strong>Total:</strong> {} agencies<br>'
                '<strong>Active:</strong> {} agencies<br>'
                '<strong>Inactive:</strong> {} agencies<br>'
                '<strong>Deleted:</strong> {} agencies'
                '</div>',
                total, active, inactive, deleted
            )
        return format_html('<em>{}</em>', _('Save to see agency statistics'))
    
    detailed_agency_stats.short_description = _('Agency Statistics')
    
    # ===== QUERYSET OPTIMIZATION =====
    
    def get_queryset(self, request):
        """
        Optimize queryset with related data.
        """
        queryset = super().get_queryset(request)
        # Annotate with agency count for efficient filtering and display
        queryset = queryset.annotate(
            agency_count=Count('agencies', filter=Q(agencies__is_deleted=False))
        )
        return queryset
    
    # ===== BULK ACTIONS =====
    
    def make_premium(self, request, queryset):
        """
        Bulk action to mark selected agency types as premium.
        """
        updated = queryset.update(is_premium=True)
        self.message_user(
            request,
            f'{updated} agency types were marked as premium.',
            level='success'
        )
    
    make_premium.short_description = _('Mark selected types as premium')
    
    def make_standard(self, request, queryset):
        """
        Bulk action to mark selected agency types as standard.
        """
        updated = queryset.update(is_premium=False)
        self.message_user(
            request,
            f'{updated} agency types were marked as standard.',
            level='success'
        )
    
    make_standard.short_description = _('Mark selected types as standard')
    
    def reset_sort_order(self, request, queryset):
        """
        Bulk action to reset sort order based on name alphabetically.
        """
        sorted_types = list(queryset.order_by('name'))
        for i, agency_type in enumerate(sorted_types, 1):
            agency_type.sort_order = i * 10  # Leave gaps for manual reordering
            agency_type.save(update_fields=['sort_order'])
        
        self.message_user(
            request,
            f'Sort order reset for {len(sorted_types)} agency types.',
            level='success'
        )
    
    reset_sort_order.short_description = _('Reset sort order alphabetically')
    
    def create_default_types(self, request, queryset):
        """
        Action to create default agency types using the model method.
        """
        try:
            created_types = AgencyType.create_default_types()
            
            if created_types:
                self.message_user(
                    request,
                    f'Created {len(created_types)} default agency types.',
                    level='success'
                )
            else:
                self.message_user(
                    request,
                    'No new agency types were created (they may already exist).',
                    level='info'
                )
        except Exception as e:
            self.message_user(
                request,
                f'Error creating default types: {str(e)}',
                level='error'
            )
    
    create_default_types.short_description = _('Create default agency types')
    
    # Register actions
    actions = [
        'make_premium',
        'make_standard', 
        'reset_sort_order',
        'create_default_types'
    ]
    
    # ===== ADDITIONAL CONFIGURATION =====
    
    save_on_top = True
    preserve_filters = True


# ============================================================================
# AGENCYTEAMMEMBER ADMIN CONFIGURATION (BONUS)
# ============================================================================

@admin.register(AgencyTeamMember)
class AgencyTeamMemberAdmin(admin.ModelAdmin):
    """
    Admin configuration for AgencyTeamMember model.
    """
    
    list_display = [
        'user_display',
        'agency_link',
        'department',
        'is_primary_contact',
        'is_public',
        'years_experience_display'
    ]
    
    list_filter = [
        'department',
        'is_primary_contact',
        'is_public',
        'agency',
        'created_at'
    ]
    
    search_fields = [
        'user__first_name',
        'user__last_name',
        'user__email',
        'agency__name'
    ]
    
    ordering = ['agency__name', '-is_primary_contact', 'user__last_name']
    
    def user_display(self, obj):
        """Display user full name with email."""
        if obj.user:
            return format_html(
                '<strong>{}</strong><br><small>{}</small>',
                obj.user.get_full_name() or obj.user.username,
                obj.user.email
            )
        return format_html('<em style="color: #999;">{}</em>', _('No user assigned'))
    
    user_display.short_description = _('Team Member')
    
    def agency_link(self, obj):
        """Display agency name with link."""
        if obj.agency:
            try:
                url = reverse('admin:agencies_agency_change', args=[obj.agency.id])
                return format_html('<a href="{}">{}</a>', url, obj.agency.name)
            except:
                return obj.agency.name
        return format_html('<em style="color: #999;">{}</em>', _('No agency'))
    
    agency_link.short_description = _('Agency')
    agency_link.admin_order_field = 'agency__name'
    
    def years_experience_display(self, obj):
        """Display years of experience with formatting."""
        if obj.years_experience:
            return format_html('{} years', obj.years_experience)
        return format_html('<span style="color: #999;">-</span>')
    
    years_experience_display.short_description = _('Experience')
    years_experience_display.admin_order_field = 'years_experience'


# ============================================================================
# REGISTER REMAINING MODELS (IF NOT ALREADY REGISTERED)
# ============================================================================

# Ensure all models are registered
if not admin.site.is_registered(Agency):
    admin.site.register(Agency, AgencyAdmin)

if not admin.site.is_registered(Brand):
    admin.site.register(Brand, BrandAdmin)

if not admin.site.is_registered(Advertiser):
    admin.site.register(Advertiser, AdvertisersAdmin)
