from django.contrib import admin
from django.utils.safestring import mark_safe

from portfolio.models import Portfolio, Category


# Register your models here.
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('title', 'parent_category', 'category_image', 'is_active', 'order', 'slug', 'created_at')
    list_filter = ('is_active', 'created_at', 'parent')
    search_fields = ('title', 'slug', 'keywords', 'description')
    list_editable = ('is_active', 'order')
    readonly_fields = ('created_at', 'updated_at')
    prepopulated_fields = {'slug': ('title',)}

    fieldsets = (
        ('Genel Bilgiler', {
            'fields': ('title', 'parent', 'slug')
        }),
        ('SEO & Açıklama', {
            'fields': ('keywords', 'description')
        }),
        ('Görsel', {
            'fields': ('image',)
        }),
        ('Ayarlar', {
            'fields': ('order', 'is_active')
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = Category

    def parent_category(self, obj):
        if obj.parent:
            return obj.parent.title
        return "Ana Kategori"

    parent_category.short_description = 'Üst Kategori'

    def category_image(self, obj):
        if obj.image:
            return mark_safe(
                '<img src="{}" width="50" height="50" style="object-fit: cover; border-radius: 5px;" title="{}" />'.format(
                    obj.image.url, obj.title))
        return mark_safe('<span style="color: #999;">Resim Yok</span>')

    category_image.short_description = 'Görsel'


admin.site.register(Category, CategoryAdmin)


class PortfolioAdmin(admin.ModelAdmin):
    list_display = ('get_portfolio_title', 'portfolio_image', 'portfolio_categories', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at', 'categories')
    search_fields = ('url',)
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')
    filter_horizontal = ('categories',)

    fieldsets = (
        ('Portfolio Bilgileri', {
            'fields': ('url', 'categories')
        }),
        ('Görsel', {
            'fields': ('image',)
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = Portfolio

    def get_portfolio_title(self, obj):
        if obj.url:
            return obj.url[:50] + '...' if len(obj.url) > 50 else obj.url
        return f"Portfolio #{obj.id}"

    get_portfolio_title.short_description = 'Portfolio'

    def portfolio_image(self, obj):
        if obj.image:
            return mark_safe(
                '<a href="{}" target="_blank"><img src="{}" width="80" height="60" style="object-fit: cover; border-radius: 5px; cursor: pointer;" title="Büyük görseli görmek için tıklayın" /></a>'.format(
                    obj.image.url, obj.image.url))
        return mark_safe('<span style="color: #999;">Resim Yok</span>')

    portfolio_image.short_description = 'Görsel'

    def portfolio_categories(self, obj):
        categories = obj.categories.all()
        if categories:
            html = '<ul style="margin: 0; padding-left: 20px;">'
            for category in categories:
                html += f'<li>{category.title}</li>'
            html += '</ul>'
            return mark_safe(html)
        return mark_safe('<span style="color: #999;">Kategori Yok</span>')

    portfolio_categories.short_description = 'Kategoriler'


admin.site.register(Portfolio, PortfolioAdmin)
