from django.contrib import admin
from django.utils.html import format_html
from django.utils.text import slugify
from .models import Leader, Partner, LeadershipCategory

@admin.register(LeadershipCategory)
class LeadershipCategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'parent', 'order', 'is_active', 'leader_count')
    list_editable = ('order', 'is_active')
    search_fields = ('name', 'description')
    list_filter = ('is_active', 'parent')
    readonly_fields = ('slug', 'created_at', 'updated_at')
    
    fieldsets = (
        ('Category Information', {
            'fields': ('name', 'description', 'parent')
        }),
        ('Settings', {
            'fields': ('order', 'is_active', 'slug')
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )
    
    def save_model(self, request, obj, form, change):
        if not obj.slug:
            obj.slug = slugify(obj.name)
        super().save_model(request, obj, form, change)
    
    def leader_count(self, obj):
        return obj.leaders.count()
    leader_count.short_description = 'Leaders'

@admin.register(Leader)
class LeaderAdmin(admin.ModelAdmin):
    list_display = ('image_preview', 'name', 'title', 'category', 'order', 'is_active')
    list_editable = ('order', 'is_active')
    search_fields = ('name', 'title', 'description')
    list_filter = ('is_active', 'category')
    readonly_fields = ('image_preview_large', 'created_at', 'updated_at')

    fieldsets = (
        ('Personal Information', {
            'fields': ('name', 'title', 'role', 'description', 'category', 'organization')
        }),
        ('Image', {
            'fields': ('image', 'image_preview_large'),
            'description': 'Upload a square profile photo (recommended: 400x400px)'
        }),
        ('Social Media', {
            'fields': (
                'linkedin_url',
                'twitter_url',
                'facebook_url',
                'instagram_url',
                'website_url'
            ),
            'description': 'Add social media profile URLs (optional)'
        }),
        ('Contact Information', {
            'fields': ('email', 'phone'),
            'description': 'Add contact information (optional)'
        }),
        ('Settings', {
            'fields': ('order', 'is_active')
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )

    def image_preview(self, obj):
        if obj.image:
            return format_html(
                '<img src="{}" style="width: 50px; height: 50px; object-fit: cover; border-radius: 50%;">',
                obj.image.url
            )
        return format_html(
            '<div style="width: 50px; height: 50px; border-radius: 50%; background: #f0f0f0; display: flex; align-items: center; justify-content: center;">'
            '<i class="fas fa-user" style="font-size: 24px; color: #999;"></i></div>'
        )
    image_preview.short_description = 'Photo'

    def image_preview_large(self, obj):
        if obj.image:
            return format_html(
                '''
                <div style="margin: 10px 0;">
                    <img src="{}" style="width: 200px; height: 200px; object-fit: cover; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
                    <p class="help" style="margin-top: 5px;">Current dimensions: {}x{} pixels</p>
                </div>
                ''',
                obj.image.url,
                obj.image.width if obj.image else 0,
                obj.image.height if obj.image else 0
            )
        return "No image uploaded"
    image_preview_large.short_description = 'Image Preview'

@admin.register(Partner)
class PartnerAdmin(admin.ModelAdmin):
    list_display = ('logo_preview', 'name', 'order', 'is_active')
    list_editable = ('order', 'is_active')
    search_fields = ('name',)
    list_filter = ('is_active',)
    readonly_fields = ('logo_preview_large', 'created_at', 'updated_at')

    fieldsets = (
        ('Partner Information', {
            'fields': ('name', 'website')
        }),
        ('Logo', {
            'fields': ('logo', 'logo_preview_large'),
            'description': 'Upload a logo (recommended: 200x100px, transparent background)'
        }),
        ('Settings', {
            'fields': ('order', 'is_active')
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )

    def logo_preview(self, obj):
        if obj.logo:
            return format_html(
                '<img src="{}" style="height: 40px; max-width: 100px; object-fit: contain;">',
                obj.logo.url
            )
        return "No logo"
    logo_preview.short_description = 'Logo'

    def logo_preview_large(self, obj):
        if obj.logo:
            return format_html(
                '''
                <div style="margin: 10px 0;">
                    <img src="{}" style="max-width: 300px; max-height: 150px; object-fit: contain;">
                    <p class="help" style="margin-top: 5px;">Current dimensions: {}x{} pixels</p>
                </div>
                ''',
                obj.logo.url,
                obj.logo.width if obj.logo else 0,
                obj.logo.height if obj.logo else 0
            )
        return "No logo uploaded"
    logo_preview_large.short_description = 'Logo Preview'

    class Media:
        css = {
            'all': ('css/custom_admin.css',)
        }
