from django.contrib import admin

from tournaments.models import team_registration
from tournaments.models.device_token import DeviceToken
from tournaments.models.draw_log import DrawLog

from .models import Club, TeamScore, Tournament, CustomUser, Event, Round, GroupStage, Match, TeamRegistration, PlayerRegistration, Purchase, Court, DateInterval, FailedApiCall, Log
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from .forms import CustomUserCreationForm, CustomUserChangeForm

from .filters import TeamScoreTournamentListFilter, MatchTournamentListFilter, SimpleTournamentListFilter, MatchTypeListFilter, SimpleIndexListFilter


class CustomUserAdmin(UserAdmin):
    form = CustomUserChangeForm
    add_form = CustomUserCreationForm
    model = CustomUser
    list_display = ['email', 'username', 'is_active', 'is_staff', 'first_name', 'last_name', 'date_joined', 'event_count']
    ordering = ['-date_joined']
    fieldsets = [
        (None, {'fields': ['id', 'username', 'email', 'password', 'first_name', 'last_name', 'is_active',
            'clubs', 'country', 'phone', 'licence_id', 'umpire_code',
            'summons_message_body', 'summons_message_signature', 'summons_available_payment_methods',
            'summons_display_format', 'summons_display_entry_fee', 'summons_use_full_custom_message',
            'match_formats_default_duration', 'bracket_match_format_preference', 'group_stage_match_format_preference',
            'loser_bracket_match_format_preference', 'device_id', 'loser_bracket_mode', 'groups'
        ]}),
        ]
    add_fieldsets = [
        (
            None,
            {
                "classes": ["wide"],
                "fields": ['username', 'email', 'password1', 'password2', 'first_name', 'last_name', 'clubs', 'country', 'phone', 'licence_id', 'umpire_code', 'groups'],
            },
        ),
    ]

class EventAdmin(admin.ModelAdmin):
    list_display = ['creation_date', 'name', 'club', 'creator', 'tenup_id']
    list_filter = ['creator', 'tenup_id']
    ordering = ['-creation_date']

class TournamentAdmin(admin.ModelAdmin):
    list_display = ['display_name', 'event', 'is_private', 'start_date', 'payment', 'creator']
    list_filter = ['is_deleted', 'event__creator']
    ordering = ['-start_date']

class TeamRegistrationAdmin(admin.ModelAdmin):
    list_display = ['player_names', 'group_stage_position', 'name', 'tournament']
    list_filter = [SimpleTournamentListFilter]

class TeamScoreAdmin(admin.ModelAdmin):
    list_display = ['team_registration', 'score', 'walk_out', 'match']
    list_filter = [TeamScoreTournamentListFilter]

class RoundAdmin(admin.ModelAdmin):
    list_display = ['tournament', 'name', 'index', 'parent']
    list_filter = [SimpleTournamentListFilter, SimpleIndexListFilter]
    search_fields = ['id']
    ordering = ['parent']

class PlayerRegistrationAdmin(admin.ModelAdmin):
    list_display = ['first_name', 'last_name', 'licence_id', 'rank']
    search_fields = ('first_name', 'last_name')
    list_filter = [TeamScoreTournamentListFilter]
    ordering = ['last_name', 'first_name']

class MatchAdmin(admin.ModelAdmin):
    list_display = ['__str__', 'round', 'group_stage', 'start_date', 'end_date', 'index']
    list_filter = [MatchTypeListFilter, MatchTournamentListFilter, SimpleIndexListFilter]
    ordering = ['-group_stage', 'round']

class GroupStageAdmin(admin.ModelAdmin):
    list_display = ['tournament', 'start_date', 'index']
    list_filter = [SimpleTournamentListFilter]
    ordering = ['-start_date', 'index']

class ClubAdmin(admin.ModelAdmin):
    list_display = ['name', 'acronym', 'city', 'creator', 'events_count', 'broadcast_code']
    search_fields = ('name', 'acronym', 'city')
    ordering = ['creator']

class PurchaseAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'product_id', 'quantity', 'purchase_date', 'revocation_date', 'expiration_date']
    list_filter = ['user']
    ordering = ['-purchase_date']

class CourtAdmin(admin.ModelAdmin):
    list_display = ['index', 'name', 'club']
    ordering = ['club']

class DateIntervalAdmin(admin.ModelAdmin):
    list_display = ['court_index', 'event']

class FailedApiCallAdmin(admin.ModelAdmin):
    list_display = ['date', 'user', 'type', 'error']
    list_filter = ['user']

class LogAdmin(admin.ModelAdmin):
    list_display = ['date', 'user', 'message']
    list_filter = ['user']

class DeviceTokenAdmin(admin.ModelAdmin):
    list_display = ['user', 'value']

class DrawLogAdmin(admin.ModelAdmin):
    list_display = ['tournament', 'draw_date', 'draw_seed', 'draw_match_index', 'draw_team_position']
    list_filter = [SimpleTournamentListFilter]
    ordering = ['draw_date']

admin.site.register(CustomUser, CustomUserAdmin)
admin.site.register(Club, ClubAdmin)
admin.site.register(Event, EventAdmin)
admin.site.register(Round, RoundAdmin)
admin.site.register(GroupStage, GroupStageAdmin)
admin.site.register(Match, MatchAdmin)
admin.site.register(TeamScore, TeamScoreAdmin)
admin.site.register(TeamRegistration, TeamRegistrationAdmin)
admin.site.register(Tournament, TournamentAdmin)
admin.site.register(PlayerRegistration, PlayerRegistrationAdmin)
admin.site.register(Purchase, PurchaseAdmin)
admin.site.register(Court, CourtAdmin)
admin.site.register(DateInterval, DateIntervalAdmin)
admin.site.register(FailedApiCall, FailedApiCallAdmin)
admin.site.register(Log, LogAdmin)
admin.site.register(DeviceToken, DeviceTokenAdmin)
admin.site.register(DrawLog, DrawLogAdmin)
