from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from apps.onboarding.models import AgentConfig, OnboardingSession, AgentInteractionLog, OnboardingFlow

class AgentInteractionLogInline(admin.TabularInline):
    model = AgentInteractionLog
    extra = 0
    readonly_fields = ('sender_type', 'content', 'tool_call_metadata', 'created_at')
    can_delete = False
    
    def has_add_permission(self, request, obj=None):
        return False

@admin.register(AgentConfig)
class AgentConfigAdmin(admin.ModelAdmin):
    list_display = ('name', 'agent_type', 'organization', 'created_at')
    list_filter = ('agent_type', 'organization')
    search_fields = ('name', 'system_prompt')
    readonly_fields = ('uuid', 'created_at', 'updated_at')
    fieldsets = (
        (None, {'fields': ('name', 'agent_type', 'organization', 'uuid', 'system_prompt', 'llm_config', 'tool_permissions')}),
        (_('Agent Logic'), {'fields': ()}),
        (_('Metadata'), {'fields': ('created_at', 'updated_at')}),
    )

@admin.register(OnboardingSession)
class OnboardingSessionAdmin(admin.ModelAdmin):
    list_display = ('user', 'role', 'status', 'created_at', 'completed_at')
    list_filter = ('status', 'role', 'created_at')
    search_fields = ('user__email_address', 'role__name')
    readonly_fields = ('uuid', 'created_at', 'updated_at')
    inlines = [AgentInteractionLogInline]
    
    fieldsets = (
        (None, {'fields': ('user', 'role', 'status', 'uuid')}),
        (_('Live State'), {'fields': ('state', 'active_configs')}),
        (_('Timestamps'), {'fields': ('completed_at', 'created_at', 'updated_at')}),
    )

@admin.register(AgentInteractionLog)
class AgentInteractionLogAdmin(admin.ModelAdmin):
    list_display = ('session', 'sender_type', 'agent_config', 'created_at')
    list_filter = ('sender_type', 'created_at')
    search_fields = ('content', 'session__user__email_address')
    readonly_fields = ('uuid', 'created_at')

@admin.register(OnboardingFlow)
class OnboardingFlowAdmin(admin.ModelAdmin):
    list_display = ('title', 'role', 'is_active', 'created_at')
    list_filter = ('is_active', 'role')
    search_fields = ('title', 'role__name')
    readonly_fields = ('uuid', 'created_at', 'updated_at')
