from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from .models import Player
from .utils import get_players_data, update_players_db

def update_players_action(modeladmin, request, queryset):
    players_data = get_players_data()
    update_players_db(players_data)
    modeladmin.message_user(request, _("Players data updated successfully"))


update_players_action.short_description = _("Update players data from Sleeper API")

@admin.register(Player)
class PlayerAdmin(admin.ModelAdmin):
    def name(self, obj):
        return str(obj)

    name.short_description = _('Name')
    list_display = ('player_id', 'name', 'position', 'team')
    list_filter = ('position', 'team')
    search_fields = ('player_id', 'last_name', 'position', 'team')
    ordering = ('player_id',)
    actions = [update_players_action]
