from django.contrib import admin
from django.utils.html import format_html
from .models import (
    Prospect,
    Status,
    ProspectStatus,
    Event,
    EmailCampaign,
    EmailTracker
)

@admin.register(Prospect)
class ProspectAdmin(admin.ModelAdmin):
    list_display = ('entity_name', 'first_name', 'last_name', 'email', 'address', 'zip_code', 'city', 'created_at')
    list_filter = ('zip_code', 'created_at')
    search_fields = ('entity_name', 'first_name', 'last_name', 'email', 'zip_code', 'city')
    filter_horizontal = ('users',)
    date_hierarchy = 'created_at'

@admin.register(Status)
class StatusAdmin(admin.ModelAdmin):
    list_display = ('name', 'created_at')
    search_fields = ('name',)

@admin.register(ProspectStatus)
class ProspectStatusAdmin(admin.ModelAdmin):
    list_display = ('prospect', 'status', 'created_at')
    list_filter = ('status', 'created_at')
    search_fields = ('prospect__name', 'prospect__email')
    date_hierarchy = 'created_at'

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
    list_display = ('get_event_display', 'type', 'date', 'status', 'created_at')
    list_filter = ('type', 'status', 'date')
    search_fields = ('description',)
    filter_horizontal = ('prospects',)
    date_hierarchy = 'date'

    def get_event_display(self, obj):
        return str(obj)
    get_event_display.short_description = 'Event'

@admin.register(EmailCampaign)
class EmailCampaignAdmin(admin.ModelAdmin):
    list_display = ('subject', 'event', 'sent_at')
    list_filter = ('sent_at',)
    search_fields = ('subject', 'content')
    date_hierarchy = 'sent_at'
    readonly_fields = ('sent_at',)

@admin.register(EmailTracker)
class EmailTrackerAdmin(admin.ModelAdmin):
    list_display = (
        'campaign',
        'prospect',
        'tracking_id',
        'sent_status',
        'opened_status',
        'clicked_status'
    )
    list_filter = ('sent', 'opened', 'clicked')
    search_fields = (
        'prospect__name',
        'prospect__email',
        'campaign__subject'
    )
    readonly_fields = (
        'tracking_id', 'sent', 'sent_at',
        'opened', 'opened_at',
        'clicked', 'clicked_at'
    )
    date_hierarchy = 'sent_at'

    def sent_status(self, obj):
        return self._get_status_html(obj.sent, obj.sent_at)
    sent_status.short_description = 'Sent'
    sent_status.allow_tags = True

    def opened_status(self, obj):
        return self._get_status_html(obj.opened, obj.opened_at)
    opened_status.short_description = 'Opened'
    opened_status.allow_tags = True

    def clicked_status(self, obj):
        return self._get_status_html(obj.clicked, obj.clicked_at)
    clicked_status.short_description = 'Clicked'
    clicked_status.allow_tags = True

    def _get_status_html(self, status, date):
        if status:
            return format_html(
                '<span style="color: green;">✓</span> {}',
                date.strftime('%Y-%m-%d %H:%M') if date else ''
            )
        return format_html('<span style="color: red;">✗</span>')
