from django.contrib import admin

from .models import (
    PropertyManagerClientProfile,
    ManagedProperty,
    ManagedPropertyImage,
    MaintenanceServiceType,
    PropertyServiceSelection,
    PropertyCarePlan,
    PropertyCarePlanServiceLine,
    PropertyMaintenanceVisit,
    PropertyVisitPhoto,
    PropertyInspectionReport,
    PropertyInspectionReportPhoto,
    PropertyCareGuidanceResource,
)


class ManagedPropertyImageInline(admin.TabularInline):
    model = ManagedPropertyImage
    extra = 1

class PropertyServiceSelectionInline(admin.TabularInline):
    model = PropertyServiceSelection
    extra = 0
    fields = (
        'service_type',
        'frequency',
        'requested_visits_per_month',
        'selection_status',
        'estimated_monthly_amount_usd',
        'final_monthly_amount_usd',
    )

class PropertyCarePlanServiceLineInline(admin.TabularInline):
    model = PropertyCarePlanServiceLine
    extra = 0
    fields = (
        'service_type',
        'is_included',
        'final_frequency',
        'final_visits_per_month',
        'final_monthly_amount_usd',
        'display_order',
    )

class PropertyVisitPhotoInline(admin.TabularInline):
    model = PropertyVisitPhoto
    extra = 0

    fields = (
        'image',
        'photo_type',
        'caption',
        'is_owner_visible',
        'uploaded_by',
        'uploaded_at',
    )

    readonly_fields = (
        'uploaded_at',
    )

class PropertyInspectionReportPhotoInline(admin.TabularInline):
    model = PropertyInspectionReportPhoto
    extra = 0

    fields = (
        'image',
        'photo_type',
        'caption',
        'is_owner_visible',
        'uploaded_by',
        'uploaded_at',
    )

    readonly_fields = (
        'uploaded_at',
    )

@admin.register(PropertyManagerClientProfile)
class PropertyManagerClientProfileAdmin(admin.ModelAdmin):
    list_display = (
        'full_name',
        'email',
        'client_type',
        'residency_status',
        'current_country',
        'is_identity_verified',
        'is_approved',
        'created_at',
    )

    list_filter = (
        'client_type',
        'residency_status',
        'is_identity_verified',
        'is_approved',
        'created_at',
    )

    search_fields = (
        'full_name',
        'email',
        'whatsapp_number',
        'phone_number',
        'current_country',
    )


@admin.register(ManagedProperty)
class ManagedPropertyAdmin(admin.ModelAdmin):
    list_display = (
        'property_name',
        'owner',
        'property_category',
        'city',
        'district',
        'occupancy_status',

        'review_status',
        'is_submitted_for_review',
        'is_approved',
        'is_active',

        'submitted_for_review_at',
        'reviewed_at',
        'reviewed_by',

        'created_at',
    )

    list_filter = (
        'property_category',
        'occupancy_status',
        'current_condition',

        'review_status',
        'is_submitted_for_review',
        'is_approved',
        'is_active',

        'submitted_for_review_at',
        'reviewed_at',
        'created_at',
    )

    search_fields = (
        'property_name',
        'owner__full_name',
        'owner__email',
        'city',
        'district',
        'address',

        'reviewed_by__username',
        'reviewed_by__first_name',
        'reviewed_by__last_name',
        'reviewed_by__email',

        'staff_review_note',
    )

    readonly_fields = (
        'created_at',
        'submitted_for_review_at',
        'reviewed_at',
    )

    fieldsets = (
        (
            'Property Owner',
            {
                'fields': (
                    'owner',
                )
            }
        ),

        (
            'Property Identity',
            {
                'fields': (
                    'property_name',
                    'property_category',
                    'occupancy_status',
                )
            }
        ),

        (
            'Location Details',
            {
                'fields': (
                    'city',
                    'district',
                    'address',
                    'google_map_link',
                )
            }
        ),

        (
            'Land Details',
            {
                'fields': (
                    'land_size',
                    'land_size_unit',
                )
            }
        ),

        (
            'Building Details',
            {
                'fields': (
                    'number_of_buildings',
                    'total_floor_area',
                    'floor_area_unit',
                    'number_of_storeys',
                    'bedrooms',
                    'bathrooms',
                    'kitchens',
                    'living_rooms',
                    'other_rooms',
                )
            }
        ),

        (
            'Condition & Structure',
            {
                'fields': (
                    'property_age',
                    'current_condition',
                    'has_boundary_wall_or_fence',
                    'has_gate',
                    'has_garden_or_landscape',
                    'needs_grass_cutting',
                )
            }
        ),

        (
            'Utilities & Access',
            {
                'fields': (
                    'electricity_active',
                    'water_active',
                    'internet_active',
                    'needs_key_holding_service',
                    'allows_emergency_access',
                )
            }
        ),

        (
            'Owner Notes',
            {
                'fields': (
                    'owner_notes',
                )
            }
        ),

        (
            'Review Workflow',
            {
                'fields': (
                    'review_status',
                    'is_submitted_for_review',
                    'is_approved',
                    'submitted_for_review_at',
                    'reviewed_at',
                    'reviewed_by',
                    'staff_review_note',
                )
            }
        ),

        (
            'Record Status',
            {
                'fields': (
                    'is_active',
                    'created_at',
                )
            }
        ),
    )

    date_hierarchy = 'created_at'

    ordering = (
        '-created_at',
    )

    list_per_page = 25

    inlines = [
        ManagedPropertyImageInline,
        PropertyServiceSelectionInline,
    ]


@admin.register(ManagedPropertyImage)
class ManagedPropertyImageAdmin(admin.ModelAdmin):
    list_display = (
        'property',
        'caption',
        'order',
    )

    search_fields = (
        'property__property_name',
        'caption',
    )

@admin.register(MaintenanceServiceType)
class MaintenanceServiceTypeAdmin(admin.ModelAdmin):
    list_display = (
        'name',
        'category',
        'calculation_method',
        'base_monthly_price_usd',
        'price_per_unit_usd',
        'is_calculator_enabled',
        'requires_staff_quote',
        'is_active',
        'display_order',
    )

    list_filter = (
        'category',
        'calculation_method',
        'is_calculator_enabled',
        'requires_staff_quote',
        'is_active',
    )

    search_fields = (
        'name',
        'code',
        'short_description',
        'description',
    )

    ordering = (
        'display_order',
        'category',
        'name',
    )

    list_editable = (
        'display_order',
        'is_active',
        'is_calculator_enabled',
        'requires_staff_quote',
    )

@admin.register(PropertyServiceSelection)
class PropertyServiceSelectionAdmin(admin.ModelAdmin):
    list_display = (
        'property',
        'service_type',
        'frequency',
        'requested_visits_per_month',
        'selection_status',
        'estimated_monthly_amount_usd',
        'final_monthly_amount_usd',
        'created_at',
    )

    list_filter = (
        'service_type',
        'frequency',
        'selection_status',
        'created_at',
    )

    search_fields = (
        'property__property_name',
        'property__owner__full_name',
        'property__owner__email',
        'service_type__name',
        'owner_note',
        'staff_note',
    )

    ordering = (
        '-created_at',
    )

    list_per_page = 30

@admin.register(PropertyCarePlan)
class PropertyCarePlanAdmin(admin.ModelAdmin):
    list_display = (
        'property',
        'plan_status',
        'provisional_estimate_snapshot_usd',
        'final_monthly_fee_usd',
        'start_date',
        'finalized_at',
        'finalized_by',
        'created_at',
    )

    list_filter = (
        'plan_status',
        'start_date',
        'finalized_at',
        'created_at',
    )

    search_fields = (
        'property__property_name',
        'property__owner__full_name',
        'property__owner__email',
        'plan_notes',
        'general_exclusions',
    )

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

    fieldsets = (
        (
            'Property',
            {
                'fields': (
                    'property',
                )
            }
        ),

        (
            'Plan Status & Pricing',
            {
                'fields': (
                    'plan_status',
                    'provisional_estimate_snapshot_usd',
                    'final_monthly_fee_usd',
                    'start_date',
                )
            }
        ),

        (
            'Plan Notes & Exclusions',
            {
                'fields': (
                    'general_exclusions',
                    'plan_notes',
                )
            }
        ),

        (
            'Staff Tracking',
            {
                'fields': (
                    'created_by',
                    'updated_by',
                    'finalized_by',
                    'finalized_at',
                    'created_at',
                    'updated_at',
                )
            }
        ),
    )

    ordering = (
        '-created_at',
    )

    list_per_page = 25

    inlines = [
        PropertyCarePlanServiceLineInline,
    ]

@admin.register(PropertyMaintenanceVisit)
class PropertyMaintenanceVisitAdmin(admin.ModelAdmin):
    list_display = (
        'visit_title',
        'managed_property',
        'visit_type',
        'visit_status',
        'scheduled_date',
        'scheduled_time',
        'completed_at',
        'visited_by',
        'is_owner_visible',
        'created_at',
    )

    list_filter = (
        'visit_type',
        'visit_status',
        'is_owner_visible',
        'scheduled_date',
        'completed_at',
        'created_at',
    )

    search_fields = (
        'visit_title',

        'managed_property__property_name',
        'managed_property__owner__full_name',
        'managed_property__owner__email',

        'care_plan__property__property_name',

        'related_service_type__name',

        'visited_by__username',
        'visited_by__first_name',
        'visited_by__last_name',
        'visited_by__email',

        'visit_summary',
        'owner_visible_note',
        'internal_note',
    )

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

    fieldsets = (
        (
            'Property & Plan',
            {
                'fields': (
                    'managed_property',
                    'care_plan',
                    'related_service_type',
                )
            }
        ),

        (
            'Visit Details',
            {
                'fields': (
                    'visit_title',
                    'visit_type',
                    'visit_status',
                    'scheduled_date',
                    'scheduled_time',
                    'completed_at',
                    'visited_by',
                )
            }
        ),

        (
            'Visit Notes',
            {
                'fields': (
                    'visit_summary',
                    'owner_visible_note',
                    'internal_note',
                )
            }
        ),

        (
            'Visibility & Tracking',
            {
                'fields': (
                    'is_owner_visible',
                    'created_by',
                    'created_at',
                    'updated_at',
                )
            }
        ),
    )

    date_hierarchy = 'created_at'

    ordering = (
        '-created_at',
    )

    list_per_page = 25

    list_select_related = (
        'managed_property',
        'care_plan',
        'related_service_type',
        'visited_by',
        'created_by',
    )

    inlines = [
        PropertyVisitPhotoInline,
    ]

@admin.register(PropertyVisitPhoto)
class PropertyVisitPhotoAdmin(admin.ModelAdmin):
    list_display = (
        'visit',
        'photo_type',
        'caption',
        'is_owner_visible',
        'uploaded_by',
        'uploaded_at',
    )

    list_filter = (
        'photo_type',
        'is_owner_visible',
        'uploaded_at',
    )

    search_fields = (
        'visit__visit_title',
        'visit__managed_property__property_name',
        'caption',

        'uploaded_by__username',
        'uploaded_by__first_name',
        'uploaded_by__last_name',
        'uploaded_by__email',
    )

    readonly_fields = (
        'uploaded_at',
    )

    fieldsets = (
        (
            'Visit',
            {
                'fields': (
                    'visit',
                )
            }
        ),

        (
            'Photo Details',
            {
                'fields': (
                    'image',
                    'photo_type',
                    'caption',
                    'is_owner_visible',
                )
            }
        ),

        (
            'Upload Tracking',
            {
                'fields': (
                    'uploaded_by',
                    'uploaded_at',
                )
            }
        ),
    )

    date_hierarchy = 'uploaded_at'

    ordering = (
        '-uploaded_at',
    )

    list_per_page = 30

    list_select_related = (
        'visit',
        'uploaded_by',
    )

@admin.register(PropertyInspectionReport)
class PropertyInspectionReportAdmin(admin.ModelAdmin):
    list_display = (
        'report_title',
        'managed_property',
        'visit',
        'report_status',
        'inspection_date',
        'overall_condition',
        'urgent_action_required',
        'is_published_to_owner',
        'prepared_by',
        'approved_by',
        'published_at',
        'created_at',
    )

    list_filter = (
        'report_status',
        'overall_condition',
        'cleanliness_status',
        'garden_status',
        'security_status',
        'utility_status',
        'structural_status',
        'urgent_action_required',
        'is_published_to_owner',
        'inspection_date',
        'published_at',
        'created_at',
    )

    search_fields = (
        'report_title',

        'managed_property__property_name',
        'managed_property__owner__full_name',
        'managed_property__owner__email',

        'visit__visit_title',

        'issues_found',
        'recommendations',
        'owner_visible_summary',
        'internal_staff_note',

        'prepared_by__username',
        'prepared_by__first_name',
        'prepared_by__last_name',
        'prepared_by__email',

        'approved_by__username',
        'approved_by__first_name',
        'approved_by__last_name',
        'approved_by__email',
    )

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

    fieldsets = (
        (
            'Property & Visit',
            {
                'fields': (
                    'managed_property',
                    'visit',
                )
            }
        ),

        (
            'Report Identity & Status',
            {
                'fields': (
                    'report_title',
                    'report_status',
                    'inspection_date',
                    'urgent_action_required',
                    'is_published_to_owner',
                    'published_at',
                )
            }
        ),

        (
            'Condition Assessment',
            {
                'fields': (
                    'overall_condition',
                    'cleanliness_status',
                    'garden_status',
                    'security_status',
                    'utility_status',
                    'structural_status',
                )
            }
        ),

        (
            'Findings & Recommendations',
            {
                'fields': (
                    'issues_found',
                    'recommendations',
                    'owner_visible_summary',
                    'internal_staff_note',
                )
            }
        ),

        (
            'Staff Tracking',
            {
                'fields': (
                    'prepared_by',
                    'approved_by',
                    'created_at',
                    'updated_at',
                )
            }
        ),
    )

    date_hierarchy = 'created_at'

    ordering = (
        '-created_at',
    )

    list_per_page = 25

    list_select_related = (
        'managed_property',
        'visit',
        'prepared_by',
        'approved_by',
    )

    inlines = [
        PropertyInspectionReportPhotoInline,
    ]

@admin.register(PropertyInspectionReportPhoto)
class PropertyInspectionReportPhotoAdmin(admin.ModelAdmin):
    list_display = (
        'report',
        'photo_type',
        'caption',
        'is_owner_visible',
        'uploaded_by',
        'uploaded_at',
    )

    list_filter = (
        'photo_type',
        'is_owner_visible',
        'uploaded_at',
    )

    search_fields = (
        'report__report_title',
        'report__managed_property__property_name',
        'caption',

        'uploaded_by__username',
        'uploaded_by__first_name',
        'uploaded_by__last_name',
        'uploaded_by__email',
    )

    readonly_fields = (
        'uploaded_at',
    )

    fieldsets = (
        (
            'Inspection Report',
            {
                'fields': (
                    'report',
                )
            }
        ),

        (
            'Photo Details',
            {
                'fields': (
                    'image',
                    'photo_type',
                    'caption',
                    'is_owner_visible',
                )
            }
        ),

        (
            'Upload Tracking',
            {
                'fields': (
                    'uploaded_by',
                    'uploaded_at',
                )
            }
        ),
    )

    date_hierarchy = 'uploaded_at'

    ordering = (
        '-uploaded_at',
    )

    list_per_page = 30

    list_select_related = (
        'report',
        'uploaded_by',
    )

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