from django.contrib import admin
from .models import Category, Teacher, TeachingCenter, Project, Coupon, Banner, Student, StudentCoupon, StudentHonor, StudentShowcase, Notification, StudentProject

@admin.register(Notification)
class NotificationAdmin(admin.ModelAdmin):
    list_display = ('title', 'student', 'notification_type', 'is_read', 'created_at')
    list_filter = ('notification_type', 'is_read', 'created_at')
    search_fields = ('title', 'content', 'student__name', 'student__phone')
    readonly_fields = ('created_at',)
    fieldsets = (
        (None, {
            'fields': ('student', 'title', 'content', 'notification_type')
        }),
        ('状态', {
            'fields': ('is_read', 'created_at')
        }),
    )

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'color')
    search_fields = ('name',)

@admin.register(Teacher)
class TeacherAdmin(admin.ModelAdmin):
    list_display = ('name', 'phone')
    search_fields = ('name', 'phone')

@admin.register(TeachingCenter)
class TeachingCenterAdmin(admin.ModelAdmin):
    list_display = ('name', 'address', 'phone')
    search_fields = ('name', 'phone')

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    # list_display = ('title', 'category', 'students', 'rating', 'created_at')
    list_display = ('title', 'teacher', 'students', 'address', 'created_at')
    # list_filter = ('category', 'created_at')
    list_filter = ('teacher', 'created_at')
    search_fields = ('title',)

@admin.register(Coupon)
class CouponAdmin(admin.ModelAdmin):
    list_display = ('title', 'amount', 'unit', 'expiry', 'status', 'issue_type')
    list_filter = ('status', 'expiry', 'issue_type')
    search_fields = ('title',)
    filter_horizontal = ('applicable_projects',)

@admin.register(Banner)
class BannerAdmin(admin.ModelAdmin):
    list_display = ('id', 'image', 'link', 'sort_order', 'is_active')
    list_editable = ('sort_order', 'is_active')

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    list_display = ('name', 'phone', 'responsible_teacher', 'is_active', 'status', 'teaching_center', 'created_at')
    search_fields = ('name', 'phone', 'wechat_nickname', 'openid', 'company_name', 'responsible_teacher__name')
    list_filter = ('teaching_center', 'status', 'is_active', 'responsible_teacher')
    inlines = []

class StudentProjectInline(admin.TabularInline):
    model = StudentProject
    extra = 1

class StudentCouponInline(admin.TabularInline):
    model = StudentCoupon
    extra = 1

StudentAdmin.inlines = [StudentProjectInline, StudentCouponInline]

@admin.register(StudentCoupon)
class StudentCouponAdmin(admin.ModelAdmin):
    list_display = ('student', 'coupon', 'status', 'assigned_at')
    list_filter = ('status',)
    search_fields = ('student__name', 'coupon__title')

@admin.register(StudentHonor)
class StudentHonorAdmin(admin.ModelAdmin):
    list_display = ('student', 'get_student_center', 'get_student_company', 'title', 'date', 'created_at')
    search_fields = ('student__name', 'title', 'student__company_name')
    list_filter = ('date',)

    def get_student_center(self, obj):
        return obj.student.teaching_center
    get_student_center.short_description = '教学中心'

    def get_student_company(self, obj):
        return obj.student.company_name
    get_student_company.short_description = '公司名称'

@admin.register(StudentShowcase)
class StudentShowcaseAdmin(admin.ModelAdmin):
    list_display = ('title', 'student', 'sort_order', 'is_active', 'created_at')
    list_editable = ('sort_order', 'is_active')
    search_fields = ('title', 'student__name')
