from django.contrib import admin
from .models import (
    SupportTicket, TicketStatus, TicketPriority, TicketCategory,
    TicketMessage, TicketActivity, KnowledgeBaseCategory,
    KnowledgeBaseArticle, SupportSettings, RegisteredEmail
)


@admin.register(TicketStatus)
class TicketStatusAdmin(admin.ModelAdmin):
    list_display = ['name', 'color', 'is_closed', 'is_active', 'display_order']
    list_filter = ['is_closed', 'is_active']
    search_fields = ['name']
    ordering = ['display_order', 'name']


@admin.register(TicketPriority)
class TicketPriorityAdmin(admin.ModelAdmin):
    list_display = ['name', 'level', 'color', 'sla_hours', 'is_active']
    list_filter = ['is_active']
    search_fields = ['name']
    ordering = ['level']


@admin.register(TicketCategory)
class TicketCategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'icon', 'color', 'is_active', 'display_order']
    list_filter = ['is_active']
    search_fields = ['name', 'description']
    ordering = ['display_order', 'name']


class TicketMessageInline(admin.TabularInline):
    model = TicketMessage
    extra = 0
    fields = ['message_type', 'content', 'author_name', 'created_at', 'is_internal']
    readonly_fields = ['created_at']


class TicketActivityInline(admin.TabularInline):
    model = TicketActivity
    extra = 0
    fields = ['activity_type', 'description', 'user_name', 'created_at']
    readonly_fields = ['created_at']


@admin.register(SupportTicket)
class SupportTicketAdmin(admin.ModelAdmin):
    list_display = [
        'ticket_number', 'title', 'user_name', 'user_email', 
        'status', 'priority', 'category', 'created_at', 'is_escalated'
    ]
    list_filter = ['status', 'priority', 'category', 'ticket_type', 'is_escalated', 'created_at']
    search_fields = ['ticket_number', 'title', 'user_name', 'user_email', 'description']
    readonly_fields = ['ticket_number', 'created_at', 'updated_at', 'last_activity']
    inlines = [TicketMessageInline, TicketActivityInline]
    
    fieldsets = (
        ('Ticket Information', {
            'fields': ('ticket_number', 'title', 'description', 'ticket_type')
        }),
        ('User Information', {
            'fields': ('user_name', 'user_email', 'user_phone', 'company')
        }),
        ('Ticket Management', {
            'fields': ('category', 'priority', 'status', 'assigned_to', 'assigned_at')
        }),
        ('Escalation', {
            'fields': ('is_escalated', 'escalation_reason'),
            'classes': ('collapse',)
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at', 'closed_at', 'last_activity', 'first_response_at', 'sla_deadline')
        }),
        ('Additional Information', {
            'fields': ('tags', 'internal_notes', 'attachments'),
            'classes': ('collapse',)
        }),
    )
    
    ordering = ['-created_at']


@admin.register(TicketMessage)
class TicketMessageAdmin(admin.ModelAdmin):
    list_display = ['ticket', 'message_type', 'author_name', 'created_at', 'is_internal', 'is_read']
    list_filter = ['message_type', 'is_internal', 'is_read', 'created_at']
    search_fields = ['ticket__ticket_number', 'content', 'author_name', 'author_email']
    readonly_fields = ['created_at', 'updated_at']
    ordering = ['-created_at']


@admin.register(TicketActivity)
class TicketActivityAdmin(admin.ModelAdmin):
    list_display = ['ticket', 'activity_type', 'user_name', 'created_at']
    list_filter = ['activity_type', 'created_at']
    search_fields = ['ticket__ticket_number', 'description', 'user_name']
    readonly_fields = ['created_at']
    ordering = ['-created_at']


@admin.register(KnowledgeBaseCategory)
class KnowledgeBaseCategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'icon', 'color', 'is_active', 'display_order']
    list_filter = ['is_active']
    search_fields = ['name', 'description']
    prepopulated_fields = {'slug': ('name',)}
    ordering = ['display_order', 'name']


@admin.register(KnowledgeBaseArticle)
class KnowledgeBaseArticleAdmin(admin.ModelAdmin):
    list_display = [
        'title', 'category', 'is_published', 'is_featured', 
        'view_count', 'helpful_count', 'created_at'
    ]
    list_filter = ['is_published', 'is_featured', 'category', 'created_at']
    search_fields = ['title', 'content', 'summary', 'keywords']
    prepopulated_fields = {'slug': ('title',)}
    readonly_fields = ['view_count', 'helpful_count', 'not_helpful_count', 'created_at', 'updated_at']
    
    fieldsets = (
        ('Article Information', {
            'fields': ('title', 'slug', 'category', 'content', 'summary')
        }),
        ('SEO & Metadata', {
            'fields': ('meta_description', 'keywords')
        }),
        ('Publishing', {
            'fields': ('author', 'is_published', 'is_featured', 'published_at')
        }),
        ('Statistics', {
            'fields': ('view_count', 'helpful_count', 'not_helpful_count'),
            'classes': ('collapse',)
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    ordering = ['-created_at']


@admin.register(SupportSettings)
class SupportSettingsAdmin(admin.ModelAdmin):
    list_display = ['setting_name', 'is_active', 'created_at', 'updated_at']
    list_filter = ['is_active']
    search_fields = ['setting_name', 'setting_value', 'description']
    ordering = ['setting_name']


@admin.register(RegisteredEmail)
class RegisteredEmailAdmin(admin.ModelAdmin):
    list_display = ['email', 'company_name', 'contact_name', 'is_active', 'ticket_count', 'last_ticket_date', 'created_at']
    list_filter = ['is_active', 'created_at', 'last_ticket_date']
    search_fields = ['email', 'company_name', 'contact_name', 'notes']
    readonly_fields = ['added_by', 'created_at', 'updated_at', 'last_ticket_date', 'ticket_count']
    ordering = ['-created_at']
    
    fieldsets = (
        ('Email Information', {
            'fields': ('email', 'is_active')
        }),
        ('Contact Details', {
            'fields': ('company_name', 'contact_name', 'notes')
        }),
        ('Statistics', {
            'fields': ('ticket_count', 'last_ticket_date'),
            'classes': ('collapse',)
        }),
        ('System Information', {
            'fields': ('added_by', 'created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    def save_model(self, request, obj, form, change):
        """Automatically set added_by to current user if creating new record"""
        if not change:  # If creating new object
            obj.added_by = request.user
        super().save_model(request, obj, form, change)
