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

from product.models import Product, Category, Images, Tags, ProductTree


# Register your models here.
class ProductAdmin(admin.ModelAdmin):
    list_display = ('title', 'urun_resim', 'is_active', 'urun_kategorileri', 'brim', 'price', 'slug', 'urun_galeri')
    list_filter = ('is_active', 'categories')
    search_fields = ('title', 'is_active', 'slug', 'content')
    list_editable = ('is_active', 'brim', 'price', 'slug',)
    readonly_fields = ('thumb', 'image_preview', 'thumb_preview')
    filter_horizontal = ('categories', 'tags', 'gallery')

    fieldsets = (
        ('Temel Bilgiler', {
            'fields': ('title', 'content', 'categories', 'tags')
        }),
        ('Fiyat ve Birim', {
            'fields': ('brim', 'price')
        }),
        ('SEO ve Medya', {
            'fields': ('slug', 'keywords', 'video')
        }),
        ('Görseller', {
            'fields': ('images', 'image_preview', 'thumb_preview', 'gallery'),
            'description': 'Thumb otomatik oluşturulur, images yüklediğinizde.'
        }),
        ('Durum', {
            'fields': ('is_active', 'is_front')
        }),
    )

    class Meta:
        model = Product

    def formatted_hit_count(self, obj):
        return obj.current_hit_count if obj.current_hit_count > 0 else '-'

    formatted_hit_count.admin_order_field = 'hit_count'
    formatted_hit_count.short_description = 'Hits'

    def blog_tags(self, obj):
        tags = '<ul>'
        for tag in obj.tags.all():
            tags += '<li>' + tag.tag + '</li>'
        tags += '</ul>'
        return mark_safe(tags)

    def urun_kategorileri(self, obj):
        html = '<ul>'
        for category in obj.categories.all():
            html += '<li>' + category.title + '</li>'
        html += '</ul>'
        return mark_safe(html)
        
    def urun_resim(self, obj):
        if obj.images:
            return mark_safe('<a href="/admin/product/product/{}/change/#id_images" onclick="window.location.href=\'/admin/product/product/{}/change/#id_images\'; return false;"><img src="{}" width="50" height="50" style="object-fit: cover; cursor: pointer;" title="Resmi değiştirmek için tıklayın" /></a>'.format(obj.id, obj.id, obj.images.url))
        return mark_safe('<a href="/admin/product/product/{}/change/#id_images" onclick="window.location.href=\'/admin/product/product/{}/change/#id_images\'; return false;">Resim Yok</a>'.format(obj.id, obj.id))
    
    urun_resim.short_description = 'Ürün Resmi'

    def image_preview(self, obj):
        if obj.images:
            return format_html('<img src="{0}" width="260" height="260" style="object-fit: cover; border: 1px solid #ddd;" />', obj.images.url)
        return "Resim Yok"
    image_preview.short_description = 'Ana Resim Önizleme'

    def thumb_preview(self, obj):
        if obj.thumb:
            return format_html('<img src="{0}" width="150" height="150" style="object-fit: cover; border: 1px solid #ddd;" />', obj.thumb.url)
        return "Thumb Yok (Kaydet ve otomatik oluşur)"
    thumb_preview.short_description = 'Thumb Önizleme'

    def urun_galeri(self, obj):
        html = '<ul>'
        for gal in obj.gallery.all():
            html += '<li>' + gal.title + '</li>'
        html += '</ul>'
        return mark_safe(html)


admin.site.register(Product, ProductAdmin)


class ProductTreeAdmin(admin.ModelAdmin):
    list_display = ('title', 'is_active',  'price')
    list_filter = ('is_active',)
    search_fields = ('title', 'is_active', 'content')
    list_editable = ('is_active', 'price',)

    class Meta:
        model = ProductTree

admin.site.register(ProductTree, ProductTreeAdmin)


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('title', 'parent', 'is_active', 'created_at', 'order', 'slug')
    list_filter = ('title', 'is_active', 'created_at',)
    search_fields = ('title', 'is_active', 'slug')
    list_editable = ('is_active', 'order', 'slug')

    class Meta:
        model = Category


admin.site.register(Category, CategoryAdmin)


class ImagesAdmin(admin.ModelAdmin):
    list_display = ('title', 'images', 'created_at',)
    list_filter = ('title',)
    search_fields = ('title', 'images')
    list_editable = ('images',)

    class Meta:
        model = Images


admin.site.register(Images, ImagesAdmin)


class TagsAdmin(admin.ModelAdmin):
    list_display = ('tag', 'created_at',)
    list_filter = ('tag',)
    search_fields = ('tag',)

    class Meta:
        model = Tags


admin.site.register(Tags, TagsAdmin)
