from django.contrib import admin
from unfold.admin import ModelAdmin
from .models import Competition, CompetitionEnrollment, ScoreDimension, Project, ProjectFile, Score, Comment

class ScoreDimensionInline(admin.TabularInline):
    model = ScoreDimension
    extra = 1
    tab = True

class ProjectFileInline(admin.TabularInline):
    model = ProjectFile
    extra = 0
    tab = True

@admin.register(Competition)
class CompetitionAdmin(ModelAdmin):
    list_display = ['title', 'status', 'start_time', 'end_time', 'is_active', 'created_at']
    list_filter = ['status', 'is_active']
    search_fields = ['title', 'description']
    inlines = [ScoreDimensionInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'description', 'rule_description', 'condition_description')
        }),
        ('封面设置', {
            'fields': ('cover_image', 'cover_image_url'),
            'description': '封面图可以上传本地图片，也可以填写外部链接，优先显示本地上传的图片'
        }),
        ('时间和状态', {
            'fields': ('start_time', 'end_time', 'status', 'project_visibility', 'is_active')
        }),
    )
    
    actions = ['make_published', 'make_ended']

    def make_published(self, request, queryset):
        queryset.update(status='published')
    make_published.short_description = "发布选中比赛"

    def make_ended(self, request, queryset):
        queryset.update(status='ended')
    make_ended.short_description = "结束选中比赛"

@admin.register(CompetitionEnrollment)
class CompetitionEnrollmentAdmin(ModelAdmin):
    list_display = ['competition', 'user', 'role', 'status', 'created_at']
    list_filter = ['competition', 'role', 'status']
    search_fields = ['user__nickname', 'competition__title']
    actions = ['approve_enrollment', 'reject_enrollment']

    def approve_enrollment(self, request, queryset):
        queryset.update(status='approved')
    approve_enrollment.short_description = "通过审核"

    def reject_enrollment(self, request, queryset):
        queryset.update(status='rejected')
    reject_enrollment.short_description = "拒绝申请"

@admin.register(Project)
class ProjectAdmin(ModelAdmin):
    list_display = ['title', 'competition', 'contestant', 'status', 'final_score', 'created_at']
    list_filter = ['competition', 'status']
    search_fields = ['title', 'contestant__user__nickname']
    inlines = [ProjectFileInline]
    readonly_fields = ['final_score']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('competition', 'contestant', 'title', 'description', 'team_info')
        }),
        ('封面设置', {
            'fields': ('cover_image', 'cover_image_url'),
            'description': '封面图可以上传本地图片，也可以填写外部链接，优先显示本地上传的图片'
        }),
        ('状态和得分', {
            'fields': ('status', 'final_score')
        }),
    )

@admin.register(Score)
class ScoreAdmin(ModelAdmin):
    list_display = ['project', 'judge', 'dimension', 'score', 'created_at']
    list_filter = ['project__competition', 'dimension']
    search_fields = ['project__title', 'judge__user__nickname']

@admin.register(Comment)
class CommentAdmin(ModelAdmin):
    list_display = ['project', 'judge', 'content_preview', 'created_at']
    list_filter = ['project__competition']
    search_fields = ['project__title', 'judge__user__nickname', 'content']

    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = "评语内容"
