# students/admin.py
from django.contrib import admin
from django.utils.html import format_html
from .models import Student, ParentGuardian

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    list_display = (
        'student_id', 
        'full_name_display', 
        'age_display', 
        'gender', 
        'current_class', 
        'status_display',
        'contact_info',
        'photo_preview'
    )
    
    list_filter = (
        'status', 
        'gender', 
        'current_class', 
        'city', 
        'quarter',
        'admission_date',
    )
    
    search_fields = (
        'student_id', 
        'first_name', 
        'last_name', 
        'phone_number',
        'email',
        'emergency_contact_name',
        'emergency_contact_phone',
    )
    
    readonly_fields = (
        'age_display',
        'photo_preview',
        'created_at',
        'updated_at',
    )
    
    fieldsets = (
        ('Identification', {
            'fields': (
                'student_id', 
                'first_name', 
                'last_name', 
                'date_of_birth',
                'age_display',
                'gender',
                'photo',
                'photo_preview',
            )
        }),
        
        ('Contact Information', {
            'fields': (
                'home_address',
                'quarter',
                'city',
                'phone_number',
                'email',
            )
        }),
        
        ('Emergency Contact', {
            'fields': (
                'emergency_contact_name',
                'emergency_contact_phone',
                'emergency_contact_relationship',
            )
        }),
        
        ('Medical Information', {
            'fields': (
                'blood_group',
                'allergies',
                'chronic_conditions',
                'medications',
            ),
            'classes': ('collapse',),
        }),
        
        ('School Information', {
            'fields': (
                'admission_date',
                'admission_class',
                'current_class',
                'status',
                'birth_certificate',
            )
        }),
        
        ('Additional Information', {
            'fields': ('notes',),
            'classes': ('collapse',),
        }),
        
        ('Metadata', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',),
        }),
    )
    
    # Custom methods for display
    def full_name_display(self, obj):
        return obj.get_full_name()
    full_name_display.short_description = 'Name'
    full_name_display.admin_order_field = 'last_name'
    
    def age_display(self, obj):
        return obj.get_age()
    age_display.short_description = 'Age'
    
    def status_display(self, obj):
        colors = {
            'active': 'green',
            'inactive': 'gray',
            'graduated': 'blue',
            'transferred': 'orange',
            'suspended': 'red',
        }
        color = colors.get(obj.status, 'black')
        return format_html(
            '<span style="color: {}; font-weight: bold;">{}</span>',
            color,
            obj.get_status_display()
        )
    status_display.short_description = 'Status'
    
    def contact_info(self, obj):
        return format_html(
            '{}<br>Emergency: {}',
            obj.phone_number if obj.phone_number else 'No phone',
            obj.emergency_contact_phone
        )
    contact_info.short_description = 'Contact Information'
    
    def photo_preview(self, obj):
        if obj.photo:
            return format_html(
                '<img src="{}" width="100" height="100" style="border-radius: 5px;" />',
                obj.photo.url
            )
        return "No photo"
    photo_preview.short_description = 'Photo Preview'


@admin.register(ParentGuardian)
class ParentGuardianAdmin(admin.ModelAdmin):
    list_display = (
        'full_name_display',
        'student_display',
        'relationship',
        'contact_info',
        'mobile_money_info',
        'is_primary_contact_display',
        'communication_preferences',
    )
    
    list_filter = (
        'relationship',
        'is_primary_contact',
        'mobile_money_provider',
        'preferred_language',
        'receive_sms',
        'receive_email',
        'receive_whatsapp',
    )
    
    search_fields = (
        'first_name',
        'last_name',
        'phone',
        'email',
        'student__first_name',
        'student__last_name',
        'student__student_id',
    )
    
    readonly_fields = ('created_at', 'updated_at')
    
    fieldsets = (
        ('Basic Information', {
            'fields': (
                'student',
                'first_name',
                'last_name',
                'relationship',
            )
        }),
        
        ('Contact Information', {
            'fields': (
                'phone',
                'email',
                'address',
            )
        }),
        
        ('Personal Information', {
            'fields': (
                'occupation',
                'employer',
                'education_level',
            ),
            'classes': ('collapse',),
        }),
        
        ('Mobile Money Information', {
            'fields': (
                'mobile_money_number',
                'mobile_money_provider',
            )
        }),
        
        ('Communication Preferences', {
            'fields': (
                'preferred_language',
                'receive_sms',
                'receive_email',
                'receive_whatsapp',
            )
        }),
        
        ('Administrative', {
            'fields': (
                'is_primary_contact',
                'can_pick_up',
                'notes',
            )
        }),
        
        ('Metadata', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',),
        }),
    )
    
    # Custom methods for display
    def full_name_display(self, obj):
        return obj.get_full_name()
    full_name_display.short_description = 'Parent/Guardian'
    full_name_display.admin_order_field = 'last_name'
    
    def student_display(self, obj):
        return str(obj.student)
    student_display.short_description = 'Student'
    student_display.admin_order_field = 'student__last_name'
    
    def contact_info(self, obj):
        info = f"📞 {obj.phone}"
        if obj.email:
            info += f"<br>📧 {obj.email}"
        return format_html(info)
    contact_info.short_description = 'Contact'
    
    def mobile_money_info(self, obj):
        if obj.mobile_money_provider != 'none' and obj.mobile_money_number:
            return format_html(
                '💰 {}: {}',
                obj.get_mobile_money_provider_display(),
                obj.mobile_money_number
            )
        return "No mobile money"
    mobile_money_info.short_description = 'Mobile Money'
    
    def is_primary_contact_display(self, obj):
        if obj.is_primary_contact:
            return format_html(
                '<span style="color: green; font-weight: bold;">✓ Primary</span>'
            )
        return format_html('<span style="color: gray;">Secondary</span>')
    is_primary_contact_display.short_description = 'Primary'
    
    def communication_preferences(self, obj):
        methods = []
        if obj.receive_sms:
            methods.append('SMS')
        if obj.receive_email:
            methods.append('Email')
        if obj.receive_whatsapp:
            methods.append('WhatsApp')
        
        language = obj.get_preferred_language_display()
        return format_html(
            '{}<br><small>{}</small>',
            ', '.join(methods) if methods else 'None',
            language
        )
    communication_preferences.short_description = 'Communication'