from django.contrib import admin
from .models import Resource, Event, Participant


@admin.register(Resource)
class ResourceAdmin(admin.ModelAdmin):
    """Admin interface for Resource model"""
    list_display = [
        'name', 'max_concurrent_events', 'buffer_duration',
        'is_active', 'created_at'
    ]
    list_filter = ['is_active', 'max_concurrent_events']
    search_fields = ['name', 'description']
    readonly_fields = ['created_at', 'updated_at']
    
    fieldsets = [
        ('Basic Information', {
            'fields': ('name', 'description', 'is_active')
        }),
        ('Capacity Configuration', {
            'fields': ('max_concurrent_events', 'buffer_duration'),
            'description': 'max_concurrent_events: 0=unlimited, 1=exclusive, >1=limited overlap'
        }),
        ('Metadata', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    ]


class ParticipantInline(admin.TabularInline):
    """Inline for Event participants"""
    model = Participant
    extra = 1
    readonly_fields = ['created_at']
    raw_id_fields = ['content_type']


@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
    """Admin interface for Event model"""
    list_display = [
        'title', 'start_time', 'end_time', 'status',
        'status_badge', 'created_at'
    ]
    list_filter = ['status', 'start_time']
    search_fields = ['title', 'notes']
    readonly_fields = ['created_at', 'updated_at', 'created_by']
    inlines = [ParticipantInline]
    date_hierarchy = 'start_time'
    
    fieldsets = [
        ('Event Details', {
            'fields': ('title', 'notes')
        }),
        ('Scheduling', {
            'fields': ('start_time', 'end_time', 'status')
        }),
        ('Metadata', {
            'fields': ('created_at', 'updated_at', 'created_by'),
            'classes': ('collapse',)
        }),
    ]
    
    def status_badge(self, obj):
        """Color-coded status badge"""
        colors = {
            Event.Status.SCHEDULED: '#28a745',  # green
            Event.Status.COMPLETED: '#007bff',  # blue
            Event.Status.CANCELED: '#dc3545',   # red
            Event.Status.PAID: '#ffc107',       # yellow
        }
        color = colors.get(obj.status, '#6c757d')
        return f'<span style="background:{color};color:white;padding:3px 8px;border-radius:3px;">{obj.get_status_display()}</span>'
    
    status_badge.short_description = 'Status'
    status_badge.allow_tags = True


@admin.register(Participant)
class ParticipantAdmin(admin.ModelAdmin):
    """Admin interface for Participant model"""
    list_display = [
        'event', 'role', 'content_type', 'object_id',
        'participant_display', 'created_at'
    ]
    list_filter = ['role', 'content_type']
    search_fields = ['event__title']
    readonly_fields = ['created_at', 'participant_display']
    raw_id_fields = ['event', 'content_type']
    
    def participant_display(self, obj):
        """Display the linked object"""
        return str(obj.content_object) if obj.content_object else 'N/A'
    
    participant_display.short_description = 'Participant'
