from django.contrib import admin
from .models import MotionEvent, MotionVote


class MotionVoteInline(admin.TabularInline):
    model = MotionVote
    extra = 0
    readonly_fields = ['registration_code', 'full_name', 'vote', 'voted_at', 'ip_address']
    can_delete = True
    ordering = ['-voted_at']


@admin.register(MotionEvent)
class MotionEventAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'is_active', 'is_archived', 'yes_count', 'no_count', 'total_votes', 'created_at']
    list_filter = ['is_active', 'is_archived']
    search_fields = ['name', 'slug']
    readonly_fields = ['slug', 'yes_count', 'no_count', 'total_votes', 'created_at', 'updated_at']
    inlines = [MotionVoteInline]

    def yes_count(self, obj):
        return obj.yes_count
    yes_count.short_description = 'YES'

    def no_count(self, obj):
        return obj.no_count
    no_count.short_description = 'NO'

    def total_votes(self, obj):
        return obj.total_votes
    total_votes.short_description = 'Total'


@admin.register(MotionVote)
class MotionVoteAdmin(admin.ModelAdmin):
    list_display = ['full_name', 'registration_code', 'vote', 'event', 'voted_at', 'ip_address']
    list_filter = ['event', 'vote']
    search_fields = ['registration_code', 'full_name']
    readonly_fields = ['voted_at', 'ip_address']
    ordering = ['-voted_at']
