"""
NOMAZRA Django Admin — All 11 models registered with full admin features.
"""
from django.contrib import admin
from .models import (
    Conversation, Message, Document, LawEntry, RightsCard,
    GlossaryTerm, VoiceSession, Bookmark, Reminder, Feedback, UserStats,
)


@admin.register(Conversation)
class ConversationAdmin(admin.ModelAdmin):
    list_display = ['title', 'user', 'country', 'legal_area', 'is_voice_session', 'created_at']
    list_filter = ['country', 'legal_area', 'is_voice_session', 'created_at']
    search_fields = ['title', 'user__email']
    readonly_fields = ['id', 'created_at', 'updated_at']
    raw_id_fields = ['user']


@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    list_display = ['short_content', 'role', 'conversation', 'input_type', 'tokens_used', 'created_at']
    list_filter = ['role', 'input_type', 'created_at']
    search_fields = ['content']
    readonly_fields = ['id', 'created_at']
    raw_id_fields = ['conversation']

    def short_content(self, obj):
        return obj.content[:80]
    short_content.short_description = 'Content'


@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin):
    list_display = ['original_name', 'user', 'file_type', 'status', 'created_at']
    list_filter = ['file_type', 'status', 'created_at']
    search_fields = ['original_name', 'user__email']
    readonly_fields = ['id', 'created_at']
    raw_id_fields = ['user', 'conversation']


@admin.register(LawEntry)
class LawEntryAdmin(admin.ModelAdmin):
    list_display = ['title', 'country', 'area', 'is_active', 'effective_date', 'updated_at']
    list_filter = ['country', 'area', 'is_active']
    search_fields = ['title', 'full_text', 'summary']
    readonly_fields = ['id', 'created_at', 'updated_at']
    list_editable = ['is_active']


@admin.register(RightsCard)
class RightsCardAdmin(admin.ModelAdmin):
    list_display = ['title', 'country', 'category', 'is_active', 'created_at']
    list_filter = ['country', 'category', 'is_active']
    search_fields = ['title']
    readonly_fields = ['id', 'created_at', 'updated_at']
    list_editable = ['is_active']


@admin.register(GlossaryTerm)
class GlossaryTermAdmin(admin.ModelAdmin):
    list_display = ['term', 'area', 'country', 'created_at']
    list_filter = ['area', 'country']
    search_fields = ['term', 'definition', 'plain_language']
    readonly_fields = ['id', 'created_at']


@admin.register(VoiceSession)
class VoiceSessionAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'duration_seconds', 'created_at']
    list_filter = ['created_at']
    search_fields = ['user__email', 'transcript']
    readonly_fields = ['id', 'created_at']
    raw_id_fields = ['user', 'conversation']


@admin.register(Bookmark)
class BookmarkAdmin(admin.ModelAdmin):
    list_display = ['user', 'message', 'note_preview', 'created_at']
    list_filter = ['created_at']
    search_fields = ['user__email', 'note']
    readonly_fields = ['id', 'created_at']
    raw_id_fields = ['user', 'message']

    def note_preview(self, obj):
        return obj.note[:60] if obj.note else '—'
    note_preview.short_description = 'Note'


@admin.register(Reminder)
class ReminderAdmin(admin.ModelAdmin):
    list_display = ['title', 'user', 'reminder_type', 'remind_at', 'is_completed']
    list_filter = ['reminder_type', 'is_completed', 'remind_at']
    search_fields = ['title', 'user__email']
    readonly_fields = ['id', 'created_at']
    raw_id_fields = ['user']
    list_editable = ['is_completed']


@admin.register(Feedback)
class FeedbackAdmin(admin.ModelAdmin):
    list_display = ['user', 'message', 'rating', 'comment_preview', 'created_at']
    list_filter = ['rating', 'created_at']
    search_fields = ['user__email', 'comment']
    readonly_fields = ['id', 'created_at']
    raw_id_fields = ['user', 'message']

    def comment_preview(self, obj):
        return obj.comment[:60] if obj.comment else '—'
    comment_preview.short_description = 'Comment'


@admin.register(UserStats)
class UserStatsAdmin(admin.ModelAdmin):
    list_display = ['user', 'total_queries', 'total_documents', 'total_voice_sessions', 'last_active_date']
    search_fields = ['user__email']
    readonly_fields = ['user']
    raw_id_fields = ['user']
