from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from apps.knowledge.models import TrainingFile, RoleRagDocument

@admin.register(TrainingFile)
class TrainingFileAdmin(admin.ModelAdmin):
    list_display = ('file_name', 'role', 'status', 'is_processed', 'uploaded_by', 'created_at')
    list_filter = ('status', 'is_processed', 'role__organization', 'created_at')
    search_fields = ('file_name', 'role__name', 'uploaded_by__email_address')
    raw_id_fields = ('role', 'uploaded_by')
    readonly_fields = ('uuid', 'file_size', 'file_type', 'created_at', 'updated_at')
    ordering = ('-created_at',)

@admin.register(RoleRagDocument)
class RoleRagDocumentAdmin(admin.ModelAdmin):
    list_display = ('role', 'chunk_index', 'training_file', 'is_active', 'created_at')
    list_filter = ('is_active', 'role__organization', 'created_at')
    search_fields = ('content', 'role__name', 'training_file__file_name')
    raw_id_fields = ('role', 'training_file')
    
    readonly_fields = ('uuid', 'content_hash', 'display_embedding', 'created_at', 'updated_at')
    ordering = ('role', 'chunk_index')

    def get_fields(self, request, obj=None):
        fields = super().get_fields(request, obj)
        if 'embedding' in fields:
            fields.remove('embedding')
        return fields

    @admin.display(description=_("Embedding Preview (1536d)"))
    def display_embedding(self, obj):
        if obj.embedding is not None:
            preview = list(obj.embedding[:5])
            return f"Vector({len(obj.embedding)}): {preview}... [Truncated]"
        return _("No embedding generated")