from django.contrib import admin
from igny8_core.admin.base import SiteSectorAdminMixin
from .models import Tasks, Images, Content


@admin.register(Tasks)
class TasksAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
    list_display = ['title', 'site', 'sector', 'status', 'cluster', 'content_type', 'word_count', 'created_at']
    list_filter = ['status', 'content_type', 'content_structure', 'site', 'sector']
    search_fields = ['title', 'keywords']
    ordering = ['-created_at']
    
    def get_site_display(self, obj):
        """Safely get site name"""
        try:
            return obj.site.name if obj.site else '-'
        except:
            return '-'
    get_site_display.short_description = 'Site'
    
    def get_sector_display(self, obj):
        """Safely get sector name"""
        try:
            return obj.sector.name if obj.sector else '-'
        except:
            return '-'
    
    def get_cluster_display(self, obj):
        """Safely get cluster name"""
        try:
            return obj.cluster.name if obj.cluster else '-'
        except:
            return '-'
    get_cluster_display.short_description = 'Cluster'


@admin.register(Images)
class ImagesAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
    list_display = ['get_content_title', 'site', 'sector', 'image_type', 'status', 'position', 'created_at']
    list_filter = ['image_type', 'status', 'site', 'sector']
    search_fields = ['content__title', 'content__meta_title', 'task__title']
    ordering = ['-id']  # Sort by ID descending (newest first)
    
    def get_content_title(self, obj):
        """Get content title, fallback to task title if no content"""
        if obj.content:
            return obj.content.title or obj.content.meta_title or f"Content #{obj.content.id}"
        elif obj.task:
            return obj.task.title or f"Task #{obj.task.id}"
        return '-'
    get_content_title.short_description = 'Content Title'
    
    def get_site_display(self, obj):
        """Safely get site name"""
        try:
            return obj.site.name if obj.site else '-'
        except:
            return '-'
    get_site_display.short_description = 'Site'
    
    def get_sector_display(self, obj):
        """Safely get sector name"""
        try:
            return obj.sector.name if obj.sector else '-'
        except:
            return '-'


@admin.register(Content)
class ContentAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
    list_display = ['task', 'site', 'sector', 'word_count', 'generated_at', 'updated_at']
    list_filter = ['generated_at', 'site', 'sector']
    search_fields = ['task__title']
    ordering = ['-generated_at']
    
    def get_site_display(self, obj):
        """Safely get site name"""
        try:
            return obj.site.name if obj.site else '-'
        except:
            return '-'
    get_site_display.short_description = 'Site'
    
    def get_sector_display(self, obj):
        """Safely get sector name"""
        try:
            return obj.sector.name if obj.sector else '-'
        except:
            return '-'

