from django.contrib import admin
from .models import *

class PropertyImageInline(admin.TabularInline):
    model = Property.featured_pictures.through  # Through model for ManyToMany
    extra = 1

@admin.register(Property)
class PropertyAdmin(admin.ModelAdmin):
    list_display = (
        'property_id', 'post_type', 'property_type', 'property_status',
        'price_with_currency', 'available_from', 'bedrooms', 'bathrooms',
        'city', 'state', 'country', 'apartment_no', 'hide_address',
        'contact_details_hidden', 'allow_direct_chat',
        'posted_by', 'agent', 'created_at'
    )
    list_filter = (
        'post_type', 'property_type', 'property_status', 'price_term', 'currency',
        'available_from', 'city', 'state', 'country', 'bedrooms', 'bathrooms'
    )
    search_fields = (
        'property_id', 'description',
        'city', 'state', 'country',
        'area_address', 'street', 'house_no', 'zip_code',
        'posted_by__username', 'posted_by__email',
    )
    date_hierarchy = 'available_from'
    ordering = ('-created_at',)

    readonly_fields = ('property_id', 'created_at', 'updated_at')

    inlines = [PropertyImageInline]

    fieldsets = (
        ('Basic Info', {
            'fields': (
                'post_type', 'property_type', 'property_status',
                'available_from', 'description',
            )
        }),
        ('Rooms & Area', {
            'fields': (
                'bedrooms', 'bathrooms', 'area', 'area_unit',
            )
        }),
        ('Address Details', {
            'fields': (
                'apartment_no', 'hide_address',
                'area_address', 'house_no', 'street', 'city', 'state', 'country', 'zip_code',
                'latitude', 'longitude',
            )
        }),
        ('Property Details', {
            'fields': (
                'property_id', 'floor_no', 'built_year', 'garages_count', 'garage_size',
            )
        }),
        ('Pricing', {
            'fields': ('price', 'price_term', 'currency',)
        }),
        ('Media', {
            'fields': ('thumbnail', 'video', 'view_360',)
        }),
        ('Contact & Posting', {
            'fields': (
                'contact_details_hidden', 'allow_direct_chat', 'posted_by','agent',
                'created_at', 'updated_at',
            )
        }),
    )

    def price_with_currency(self, obj):
        return obj.formatted_price()
    price_with_currency.short_description = 'Price'

@admin.register(PropertyImage)
class PropertyImageAdmin(admin.ModelAdmin):
    list_display = ('id', 'image', 'uploaded_at')
    readonly_fields = ('uploaded_at',)
    search_fields = ('id',)
    ordering = ('-uploaded_at',)


@admin.register(PropertyBookmark)
class PropertyBookmarkAdmin(admin.ModelAdmin):
    list_display = ('user', 'property', 'created_at')
    search_fields = ('user__email', 'property__property_id')



@admin.register(PropertyView)
class PropertyViewAdmin(admin.ModelAdmin):
    list_display = ('property', 'user', 'session_key', 'location', 'device', 'is_publisher', 'viewed_at')
    search_fields = ('property__property_id', 'user__email', 'location', 'device')
    list_filter = ('is_publisher', 'device', 'location', 'viewed_at')


admin.site.register(PropertyBoostPrice)
admin.site.register(PropertyBoost)
from .models import PropertyAddonsFee
admin.site.register(PropertyAddonsFee)

from .models import TempProperty, TempPropertyImage
admin.site.register(TempProperty)
admin.site.register(TempPropertyImage)
