from django.contrib import admin
from igny8_core.admin.base import SiteSectorAdminMixin
from .models import Keywords, Clusters, ContentIdeas


@admin.register(Clusters)
class ClustersAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
    list_display = ['name', 'site', 'sector', 'keywords_count', 'volume', 'status', 'created_at']
    list_filter = ['status', 'site', 'sector']
    search_fields = ['name']
    ordering = ['name']
    
    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(Keywords)
class KeywordsAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
    list_display = ['keyword', 'seed_keyword', 'site', 'sector', 'cluster', 'volume', 'difficulty', 'intent', 'status', 'created_at']
    list_filter = ['status', 'seed_keyword__intent', 'site', 'sector', 'seed_keyword__industry', 'seed_keyword__sector']
    search_fields = ['seed_keyword__keyword']
    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(ContentIdeas)
class ContentIdeasAdmin(SiteSectorAdminMixin, admin.ModelAdmin):
    list_display = ['idea_title', 'site', 'sector', 'description_preview', 'content_structure', 'content_type', 'status', 'keyword_cluster', 'estimated_word_count', 'created_at']
    list_filter = ['status', 'content_structure', 'content_type', 'site', 'sector']
    search_fields = ['idea_title', 'target_keywords', 'description']
    ordering = ['-created_at']
    
    def description_preview(self, obj):
        """Show a truncated preview of the description"""
        if not obj.description:
            return '-'
        # Truncate to 100 characters
        preview = obj.description[:100]
        if len(obj.description) > 100:
            preview += '...'
        return preview
    description_preview.short_description = 'Description'
    
    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_keyword_cluster_display(self, obj):
        """Safely get cluster name"""
        try:
            return obj.keyword_cluster.name if obj.keyword_cluster else '-'
        except:
            return '-'
    get_keyword_cluster_display.short_description = 'Cluster'

