from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth import get_user_model
from .models import Team

User = get_user_model()


@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
    list_display = ('name', 'total_seconds_pool', 'total_seconds_used', 'monthly_seconds_limit', 'is_active', 'member_count', 'created_at')
    list_filter = ('is_active',)
    search_fields = ('name',)

    def member_count(self, obj):
        return obj.members.count()
    member_count.short_description = '成员数'


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    list_display = ('username', 'email', 'team', 'is_team_admin', 'daily_seconds_limit', 'monthly_seconds_limit', 'is_staff', 'date_joined')
    list_filter = ('is_staff', 'is_active', 'is_team_admin', 'team')
    search_fields = ('username', 'email')
    fieldsets = BaseUserAdmin.fieldsets + (
        ('团队与配额', {'fields': ('team', 'is_team_admin', 'daily_seconds_limit', 'monthly_seconds_limit')}),
    )
