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

from home.models import Tags, Home, AboutMe, MyService, MyServiceTitle, Education, Experience, Skill, Knowledge, \
    MyResume, MainMenu


# ==================== TAGS ADMIN ====================
class TagsAdmin(admin.ModelAdmin):
    list_display = ('tag', 'is_active', 'created_at', 'updated_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('tag',)
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')

    class Meta:
        model = Tags


admin.site.register(Tags, TagsAdmin)


# ==================== HOME ADMIN ====================
class HomeAdmin(admin.ModelAdmin):
    list_display = ('name', 'title', 'home_image', 'home_tags', 'is_active', 'slug', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('name', 'title', 'slug', 'keywords')
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at', 'slug')
    filter_horizontal = ('tags',)


    fieldsets = (
        ('Genel Bilgiler', {
            'fields': ('name', 'title', 'slug')
        }),
        ('Butonlar & Video', {
            'fields': ('button1', 'button2', 'video')
        }),
        ('SEO & Etiketler', {
            'fields': ('keywords', 'tags')
        }),
        ('Görsel', {
            'fields': ('image',)
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = Home

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

    home_tags.short_description = 'Etiketler'

    def home_image(self, obj):
        if obj.image:
            return mark_safe(
                '<a href="{}" target="_blank"><img src="{}" width="50" height="50" 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>')

    home_image.short_description = 'Görsel'


admin.site.register(Home, HomeAdmin)


# ==================== ABOUT ME ADMIN ====================
class AboutMeAdmin(admin.ModelAdmin):
    list_display = ('title', 'about_image', 'city', 'age', 'is_active', 'counter_active', 'created_at')
    list_filter = ('is_active', 'counter_active', 'created_at')
    search_fields = ('title', 'city', 'website', 'mail')
    list_editable = ('is_active', 'counter_active')
    readonly_fields = ('created_at', 'updated_at', 'slug')

    fieldsets = (
        ('Genel Bilgiler', {
            'fields': ('title', 'slug')
        }),
        ('Görsel', {
            'fields': ('image', 'image_sub')
        }),
        ('CV', {
            'fields': ('cv',)
        }),
        ('Kişisel Bilgiler', {
            'fields': ('birthday', 'age', 'city', 'study', 'degree', 'interests')
        }),
        ('İletişim', {
            'fields': ('website', 'phone', 'mail', 'x')
        }),
        ('Sayaçlar', {
            'fields': ('done', 'project_done', 'user_h', 'hapy_user', 'great', 'great_reviews', 'team', 'support_team'),
            'classes': ('collapse',)
        }),
        ('Ayarlar', {
            'fields': ('is_active', 'counter_active')
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = AboutMe

    def about_image(self, obj):
        if obj.image:
            return mark_safe(
                '<a href="{}" target="_blank"><img src="{}" width="50" height="50" 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>')

    about_image.short_description = 'Görsel'


admin.site.register(AboutMe, AboutMeAdmin)


# ==================== MY SERVICE ADMIN ====================
class MyServiceAdmin(admin.ModelAdmin):
    list_display = ('title', 'service_image', 'is_active', 'slug', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('title', 'slug', 'content')
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at', 'slug')

    fieldsets = (
        ('Genel Bilgiler', {
            'fields': ('title', 'slug')
        }),
        ('İçerik', {
            'fields': ('content',)
        }),
        ('Görsel', {
            'fields': ('image',)
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = MyService

    def service_image(self, obj):
        if obj.image:
            return mark_safe(
                '<a href="{}" target="_blank"><img src="{}" width="50" height="50" 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>')

    service_image.short_description = 'Görsel'


admin.site.register(MyService, MyServiceAdmin)


# ==================== MY SERVICE TITLE ADMIN ====================
class MyServiceTitleAdmin(admin.ModelAdmin):
    list_display = ('title', 'title_sub', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('title', 'title_sub')
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')

    fieldsets = (
        ('Başlık Bilgileri', {
            'fields': ('title', 'title_sub')
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = MyServiceTitle


admin.site.register(MyServiceTitle, MyServiceTitleAdmin)


# ==================== MY RESUME ADMIN ====================
class MyResumeAdmin(admin.ModelAdmin):
    list_display = ('title', 'title_sub', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('title', 'title_sub')
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')

    fieldsets = (
        ('Genel Bilgiler', {
            'fields': ('title', 'title_sub')
        }),
        ('Bölüm Başlıkları', {
            'fields': ('education', 'experience', 'coding_skills', 'knowledge')
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = MyResume


admin.site.register(MyResume, MyResumeAdmin)


# ==================== EDUCATION ADMIN ====================
class EducationAdmin(admin.ModelAdmin):
    list_display = ('title', 'between_years', 'get_resume', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at', 'resume')
    search_fields = ('title', 'content', 'between_years')
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')

    fieldsets = (
        ('Eğitim Bilgileri', {
            'fields': ('title', 'between_years', 'content')
        }),
        ('Resume Bağlantısı', {
            'fields': ('resume',)
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = Education

    def get_resume(self, obj):
        if obj.resume:
            return obj.resume.title
        return mark_safe('<span style="color: #999;">Resume Yok</span>')

    get_resume.short_description = 'Resume'


admin.site.register(Education, EducationAdmin)


# ==================== EXPERIENCE ADMIN ====================
class ExperienceAdmin(admin.ModelAdmin):
    list_display = ('title', 'between_years', 'get_resume', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at', 'resume')
    search_fields = ('title', 'content', 'between_years')
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')

    fieldsets = (
        ('Deneyim Bilgileri', {
            'fields': ('title', 'between_years', 'content')
        }),
        ('Resume Bağlantısı', {
            'fields': ('resume',)
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = Experience

    def get_resume(self, obj):
        if obj.resume:
            return obj.resume.title
        return mark_safe('<span style="color: #999;">Resume Yok</span>')

    get_resume.short_description = 'Resume'


admin.site.register(Experience, ExperienceAdmin)


# ==================== SKILL ADMIN ====================
class SkillAdmin(admin.ModelAdmin):
    list_display = ('title', 'degree', 'get_resume', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at', 'resume')
    search_fields = ('title',)
    list_editable = ('is_active', 'degree')
    readonly_fields = ('created_at', 'updated_at')

    fieldsets = (
        ('Yetenek Bilgileri', {
            'fields': ('title', 'degree')
        }),
        ('Resume Bağlantısı', {
            'fields': ('resume',)
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = Skill

    def get_resume(self, obj):
        if obj.resume:
            return obj.resume.title
        return mark_safe('<span style="color: #999;">Resume Yok</span>')

    get_resume.short_description = 'Resume'


admin.site.register(Skill, SkillAdmin)


# ==================== KNOWLEDGE ADMIN ====================
class KnowledgeAdmin(admin.ModelAdmin):
    list_display = ('title', 'get_resume', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at', 'resume')
    search_fields = ('title',)
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')

    fieldsets = (
        ('Bilgi Başlığı', {
            'fields': ('title',)
        }),
        ('Resume Bağlantısı', {
            'fields': ('resume',)
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = Knowledge

    def get_resume(self, obj):
        if obj.resume:
            return obj.resume.title
        return mark_safe('<span style="color: #999;">Resume Yok</span>')

    get_resume.short_description = 'Resume'


admin.site.register(Knowledge, KnowledgeAdmin)


# ==================== MAIN MENU ADMIN ====================
class MainMenuAdmin(admin.ModelAdmin):
    list_display = ('home', 'about', 'services', 'resume', 'portfolio', 'contact', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('home', 'about', 'services', 'resume', 'portfolio', 'contact')
    list_editable = ('is_active',)
    readonly_fields = ('created_at', 'updated_at')

    fieldsets = (
        ('Menü İsimleri', {
            'fields': ('home', 'about', 'services', 'resume', 'portfolio', 'contact')
        }),
        ('Ayarlar', {
            'fields': ('is_active',)
        }),
        ('Tarihler', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )

    class Meta:
        model = MainMenu


admin.site.register(MainMenu, MainMenuAdmin)
