from django.contrib import admin
from .models import Ticket, TicketComment, TicketTemplate, CannedResponse, TicketEmailAddress


@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
    list_display = ('id', 'subject', 'ticket_type', 'status', 'priority', 'creator', 'assignee', 'tenant', 'created_at')
    list_filter = ('status', 'priority', 'ticket_type', 'tenant', 'created_at')
    search_fields = ('subject', 'description', 'creator__email', 'assignee__email')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('creator', 'assignee', 'tenant')
    ordering = ('-created_at',)

    fieldsets = (
        (None, {
            'fields': ('subject', 'description', 'category')
        }),
        ('Classification', {
            'fields': ('ticket_type', 'status', 'priority')
        }),
        ('Assignment', {
            'fields': ('tenant', 'creator', 'assignee')
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at', 'resolved_at'),
            'classes': ('collapse',)
        }),
    )


@admin.register(TicketComment)
class TicketCommentAdmin(admin.ModelAdmin):
    list_display = ('id', 'ticket', 'author', 'is_internal', 'created_at')
    list_filter = ('is_internal', 'created_at')
    search_fields = ('comment_text', 'author__email', 'ticket__subject')
    readonly_fields = ('created_at',)
    raw_id_fields = ('ticket', 'author')
    ordering = ('-created_at',)


@admin.register(TicketTemplate)
class TicketTemplateAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'ticket_type', 'category', 'default_priority', 'tenant', 'is_active', 'created_at')
    list_filter = ('ticket_type', 'category', 'default_priority', 'is_active', 'tenant', 'created_at')
    search_fields = ('name', 'description', 'subject_template', 'description_template')
    readonly_fields = ('created_at',)
    raw_id_fields = ('tenant',)
    ordering = ('ticket_type', 'name')

    fieldsets = (
        (None, {
            'fields': ('name', 'description', 'is_active')
        }),
        ('Template Details', {
            'fields': ('ticket_type', 'category', 'default_priority')
        }),
        ('Content Templates', {
            'fields': ('subject_template', 'description_template')
        }),
        ('Scope', {
            'fields': ('tenant',)
        }),
        ('Timestamps', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        }),
    )


@admin.register(CannedResponse)
class CannedResponseAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'category', 'tenant', 'is_active', 'use_count', 'created_by', 'created_at')
    list_filter = ('category', 'is_active', 'tenant', 'created_at')
    search_fields = ('title', 'content', 'created_by__email')
    readonly_fields = ('use_count', 'created_at')
    raw_id_fields = ('tenant', 'created_by')
    ordering = ('-use_count', 'title')

    fieldsets = (
        (None, {
            'fields': ('title', 'content', 'is_active')
        }),
        ('Categorization', {
            'fields': ('category',)
        }),
        ('Scope', {
            'fields': ('tenant',)
        }),
        ('Metadata', {
            'fields': ('use_count', 'created_by', 'created_at'),
            'classes': ('collapse',)
        }),
    )


@admin.register(TicketEmailAddress)
class TicketEmailAddressAdmin(admin.ModelAdmin):
    list_display = ('id', 'display_name', 'email_address', 'tenant', 'is_active', 'is_default', 'color', 'emails_processed_count', 'last_check_at')
    list_filter = ('is_active', 'is_default', 'tenant', 'created_at')
    search_fields = ('display_name', 'email_address', 'tenant__name')
    readonly_fields = ('last_check_at', 'last_error', 'emails_processed_count', 'created_at', 'updated_at')
    raw_id_fields = ('tenant',)
    ordering = ('tenant', '-is_default', 'display_name')

    fieldsets = (
        (None, {
            'fields': ('tenant', 'display_name', 'email_address', 'color')
        }),
        ('IMAP Settings (Inbound)', {
            'fields': ('imap_host', 'imap_port', 'imap_use_ssl', 'imap_username', 'imap_password', 'imap_folder'),
            'classes': ('collapse',)
        }),
        ('SMTP Settings (Outbound)', {
            'fields': ('smtp_host', 'smtp_port', 'smtp_use_tls', 'smtp_use_ssl', 'smtp_username', 'smtp_password'),
            'classes': ('collapse',)
        }),
        ('Status & Settings', {
            'fields': ('is_active', 'is_default')
        }),
        ('Statistics', {
            'fields': ('last_check_at', 'last_error', 'emails_processed_count'),
            'classes': ('collapse',)
        }),
        ('Timestamps', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
