from django.contrib import admin
from django.utils.html import format_html
from .models import DatingPartnerProfile, DatingInterest, DatingPartnerPhoto, DatingPartnerGuidanceResource


@admin.register(DatingPartnerProfile)
class DatingPartnerProfileAdmin(admin.ModelAdmin):
    list_display = (
        'profile_code',
        'full_name',
        'gender',
        'age',
        'city',
        'relationship_goal',
        'status',
        'is_public',
        'is_featured',
        'created_at',
    )
    list_filter = ('gender', 'relationship_goal', 'status', 'is_public', 'is_featured', 'city')
    search_fields = ('profile_code', 'full_name', 'display_name', 'email', 'city', 'languages_spoken')

@admin.register(DatingInterest)
class DatingInterestAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'foreigner_user',
        'foreigner_name',
        'partner_name',
        'partner_goal',
        'status',
        'created_at',
    )
    list_editable = ('status',)
    list_filter = (
        'status',
        'partner__relationship_goal',
        'partner__gender',
        'created_at',
    )
    search_fields = (
        'foreigner__user__username',
        'foreigner__full_name',
        'foreigner__email',
        'partner__full_name',
        'partner__display_name',
        'partner__profile_code',
        'message',
    )
    readonly_fields = ('created_at',)
    autocomplete_fields = ('foreigner', 'partner')
    ordering = ('-created_at',)
    date_hierarchy = 'created_at'

    fieldsets = (
        ('Interest Details', {
            'fields': ('foreigner', 'partner', 'status', 'message')
        }),
        ('Dates', {
            'fields': ('created_at',)
        }),
    )

    def foreigner_user(self, obj):
        return obj.foreigner.user.username if obj.foreigner and obj.foreigner.user else '-'
    foreigner_user.short_description = 'Username'

    def foreigner_name(self, obj):
        return getattr(obj.foreigner, 'full_name', '-') or '-'
    foreigner_name.short_description = 'Foreigner Name'

    def partner_name(self, obj):
        return obj.partner.display_name or obj.partner.full_name
    partner_name.short_description = 'Dating Partner'

    def partner_goal(self, obj):
        return obj.partner.get_relationship_goal_display()
    partner_goal.short_description = 'Goal'


@admin.register(DatingPartnerPhoto)
class DatingPartnerPhotoAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'photo_preview_small',
        'profile_name',
        'profile_code',
        'status',
        'is_public',
        'display_order',
        'uploaded_at',
    )
    list_editable = (
        'status',
        'is_public',
        'display_order',
    )
    list_filter = (
        'status',
        'is_public',
        'uploaded_at',
        'profile__gender',
        'profile__relationship_goal',
    )
    search_fields = (
        'profile__full_name',
        'profile__display_name',
        'profile__profile_code',
        'caption',
    )
    readonly_fields = (
        'uploaded_at',
        'photo_preview',
    )
    autocomplete_fields = (
        'profile',
    )
    ordering = (
        'display_order',
        '-uploaded_at',
    )
    date_hierarchy = 'uploaded_at'

    fieldsets = (
        ('Photo Details', {
            'fields': (
                'profile',
                'image',
                'photo_preview',
                'caption',
            )
        }),
        ('Visibility & Review', {
            'fields': (
                'status',
                'is_public',
                'display_order',
            )
        }),
        ('System Info', {
            'fields': (
                'uploaded_at',
            )
        }),
    )

    def profile_name(self, obj):
        return obj.profile.display_name or obj.profile.full_name
    profile_name.short_description = 'Dating Partner'

    def profile_code(self, obj):
        return obj.profile.profile_code
    profile_code.short_description = 'Profile Code'

    def photo_preview(self, obj):
        if obj.image:
            return format_html(
                '<img src="{}" style="max-width:200px; max-height:200px; object-fit:cover; border-radius:10px;" />',
                obj.image.url
            )
        return "No Image"
    photo_preview.short_description = 'Photo Preview'

    def photo_preview_small(self, obj):
        if obj.image:
            return format_html(
                '<img src="{}" style="width:60px; height:60px; object-fit:cover; border-radius:8px;" />',
                obj.image.url
            )
        return "—"
    photo_preview_small.short_description = 'Image'

@admin.register(DatingPartnerGuidanceResource)
class DatingPartnerGuidanceResourceAdmin(admin.ModelAdmin):
    list_display = (
        'language',
        'title',
        'is_active',
        'created_at',
        'updated_at',
    )

    list_filter = (
        'language',
        'is_active',
        'created_at',
    )

    search_fields = (
        'title',
        'description',
    )

    readonly_fields = (
        'created_at',
        'updated_at',
    )

    fieldsets = (
        ('Language & Title', {
            'fields': (
                'language',
                'title',
                'description',
                'is_active',
            )
        }),
        ('Guidance Documents', {
            'fields': (
                'full_playbook_pdf',
                'summary_overview_pdf',
            )
        }),
        ('Audio & Video Guidance', {
            'fields': (
                'audio_guide_file',
                'youtube_video_link',
            )
        }),
        ('System Information', {
            'fields': (
                'created_at',
                'updated_at',
            )
        }),
    )