from django.contrib import admin, messages

from events import models


@admin.register(models.Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', 'event')
    list_filter = ('event',)

@admin.register(models.Event)
class EventAdmin(admin.ModelAdmin):
    list_display = ('start', 'name', 'is_actual', 'is_public', 'registrations_enabled')


def presentation(modeladmin, request, queryset):
    for registration in queryset:
        registration.is_presented = True

        try:
            registration.save()
        except Exception as e:
           messages.error(request, f"Registraciu {registration} sa nepodarilo zaprezentovat: {e}")
presentation.short_description = "Zaprezentovat registraciu"

def pay(modeladmin, request, queryset):
    for registration in queryset:
        registration.is_paid = True

        try:
            registration.save()
        except Exception as e:
           messages.error(request, f"Registraciu {registration} sa nepodarilo uhradit: {e}")
pay.short_description = "Oznacit registraciu ako uhradenu"

def finish(modeladmin, request, queryset):
    for registration in queryset:
        registration.is_finished = True

        try:
            registration.save()
        except Exception as e:
           messages.error(request, f"Registraciu {registration} sa nepodarilo dokoncit: {e}")
finish.short_description = "Dokoncit registraciu"


@admin.register(models.Registration)
class RegistrationAdmin(admin.ModelAdmin):
    list_display = ('created', 'variable_symbol', 'event', 'email', 'first_name', 'last_name', 'is_confirmed' ,'is_paid', 'is_finished')
    list_filter = ('event', 'category', 'is_confirmed', 'is_paid', 'is_finished', 'email')
    search_fields = ('email', 'first_name', 'last_name', 'variable_symbol')
    actions = [presentation, pay, finish]


@admin.register(models.HTMLSection)
class HTMLSectionAdmin(admin.ModelAdmin):
    list_display = ('created', 'event', 'identifier', 'order_index')


@admin.register(models.Lap)
class LapAdmin(admin.ModelAdmin):
    list_display = ('number', 'registration', 'start', 'end', 'duration')
    list_filter = ('registration',)