from django.contrib import admin
from .models import CaseStudy, CaseStudyCategory, Client, CaseStudyImage, CaseStudyProcess


@admin.register(CaseStudyCategory)
class CaseStudyCategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'display_order', 'is_active', 'case_studies_count']
    list_filter = ['is_active']
    search_fields = ['name', 'slug']
    prepopulated_fields = {'slug': ('name',)}
    ordering = ['display_order', 'name']

    def case_studies_count(self, obj):
        return obj.case_studies.count()
    case_studies_count.short_description = 'Case Studies Count'


@admin.register(Client)
class ClientAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'website', 'is_active', 'case_studies_count']
    list_filter = ['is_active', 'created_at']
    search_fields = ['name', 'slug', 'website']
    prepopulated_fields = {'slug': ('name',)}
    ordering = ['name']

    def case_studies_count(self, obj):
        return obj.case_studies.count()
    case_studies_count.short_description = 'Case Studies Count'


class CaseStudyImageInline(admin.TabularInline):
    model = CaseStudyImage
    extra = 1
    fields = ['image', 'image_url', 'caption', 'display_order']


class CaseStudyProcessInline(admin.TabularInline):
    model = CaseStudyProcess
    extra = 1
    fields = ['step_number', 'title', 'description']
    ordering = ['step_number']


@admin.register(CaseStudy)
class CaseStudyAdmin(admin.ModelAdmin):
    list_display = [
        'title', 'category', 'client', 'published', 
        'featured', 'views_count', 'display_order', 'published_at'
    ]
    list_filter = [
        'published', 'featured', 'category', 
        'client', 'published_at', 'created_at'
    ]
    search_fields = ['title', 'description', 'excerpt']
    prepopulated_fields = {'slug': ('title',)}
    date_hierarchy = 'published_at'
    ordering = ['display_order', '-published_at', '-created_at']
    inlines = [CaseStudyImageInline, CaseStudyProcessInline]
    
    fieldsets = (
        ('Basic Information', {
            'fields': ('title', 'slug', 'subtitle', 'category', 'client')
        }),
        ('Content', {
            'fields': ('excerpt', 'description', 'project_overview', 'site_map_content')
        }),
        ('Images', {
            'fields': (
                'thumbnail', 'thumbnail_url',
                'featured_image', 'featured_image_url',
                'poster_image', 'poster_image_url',
                'project_image', 'project_image_url'
            )
        }),
        ('SEO', {
            'fields': ('meta_description', 'meta_keywords'),
            'classes': ('collapse',)
        }),
        ('Status & Visibility', {
            'fields': ('published', 'featured', 'display_order', 'published_at')
        }),
        ('Statistics', {
            'fields': ('views_count',),
            'classes': ('collapse',)
        }),
    )

    readonly_fields = ['views_count']


@admin.register(CaseStudyImage)
class CaseStudyImageAdmin(admin.ModelAdmin):
    list_display = ['case_study', 'caption', 'display_order', 'created_at']
    list_filter = ['case_study', 'created_at']
    search_fields = ['case_study__title', 'caption']
    ordering = ['case_study', 'display_order']


@admin.register(CaseStudyProcess)
class CaseStudyProcessAdmin(admin.ModelAdmin):
    list_display = ['case_study', 'step_number', 'title', 'created_at']
    list_filter = ['case_study', 'created_at']
    search_fields = ['case_study__title', 'title', 'description']
    ordering = ['case_study', 'step_number']

