from django.contrib import admin
from django.utils.html import format_html
from .models import (
    RentalOwner, RentalListing, RentalListingImage, RentalInquiry, RentalInterest, 
    RentalOwnerBankingDetails, RentalPartnerGuidanceResource
)

class RentalListingImageInline(admin.TabularInline):
    model = RentalListingImage
    extra = 1
    fields = ['image', 'caption', 'order']


@admin.register(RentalOwner)
class RentalOwnerAdmin(admin.ModelAdmin):
    list_display = [
        'full_name',
        'email',
        'city',
        'owner_type',
        'is_identity_verified',
        'is_approved',
        'is_featured',
        'can_submit_listings',
        'created_at',
    ]
    list_filter = ['owner_type', 'is_identity_verified', 'is_approved', 'is_featured', 'can_submit_listings', 'city']
    search_fields = ['full_name', 'email', 'whatsapp_number', 'phone_number', 'business_name']
    list_editable = ['is_identity_verified', 'is_approved', 'is_featured', 'can_submit_listings']


@admin.register(RentalListing)
class RentalListingAdmin(admin.ModelAdmin):
    list_display = [
        'title',
        'owner',
        'city',
        'monthly_rent_usd',
        'visibility',
        'is_available',
        'is_approved',
        'is_featured',
        'is_active',
    ]
    list_filter = [
        'property_type',
        'visibility',
        'furnishing',
        'is_available',
        'is_approved',
        'is_featured',
        'is_active',
        'city',
    ]
    search_fields = ['title', 'city', 'area', 'short_description', 'full_description']
    prepopulated_fields = {}
    list_editable = ['is_available', 'is_approved', 'is_featured', 'is_active']
    inlines = [RentalListingImageInline]


@admin.register(RentalInquiry)
class RentalInquiryAdmin(admin.ModelAdmin):
    list_display = ['full_name', 'email', 'listing', 'is_reviewed', 'created_at']
    list_filter = ['is_reviewed', 'created_at']
    search_fields = ['full_name', 'email', 'whatsapp_number', 'message', 'listing__title']
    list_editable = ['is_reviewed']

@admin.register(RentalInterest)
class RentalInterestAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'foreigner_user',
        'foreigner_name',
        'listing',
        'status',
        'created_at',
    )
    list_editable = ('status',)
    list_filter = (
        'status',
        'created_at',
    )
    search_fields = (
        'foreigner__user__username',
        'foreigner__full_name',
        'foreigner__email',
        'listing__title',
        'notes',
    )
    readonly_fields = ('created_at',)
    autocomplete_fields = ('foreigner', 'listing')
    ordering = ('-created_at',)
    date_hierarchy = 'created_at'

    fieldsets = (
        ('Interest Details', {
            'fields': ('foreigner', 'listing', 'status', 'notes')
        }),
        ('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'


@admin.register(RentalOwnerBankingDetails)
class RentalOwnerBankingDetailsAdmin(admin.ModelAdmin):
    list_display = [
        'owner',
        'bank_name',
        'account_holder_name',
        'is_verified',
        'updated_at',
    ]
    list_filter = ['is_verified', 'updated_at']
    search_fields = [
        'owner__full_name',
        'owner__email',
        'bank_name',
        'account_holder_name',
        'account_number',
    ]

@admin.register(RentalPartnerGuidanceResource)
class RentalPartnerGuidanceResourceAdmin(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',
            )
        }),
    )