from django.contrib import admin
from .models import ServicePartnerProfile, ServicePartnerInterest, ServicePartnerGuidanceResource
from django.utils.html import format_html

@admin.register(ServicePartnerProfile)
class ServicePartnerProfileAdmin(admin.ModelAdmin):
    list_display = (
        'profile_code',
        'full_name',
        'service_type',
        'city',
        'status',
        'is_public',
        'is_featured',
        'resume_uploaded',
        'created_at',
    )

    list_filter = (
        'service_type',
        'status',
        'is_public',
        'is_featured',
        'city',
        'created_at',
    )

    search_fields = (
        'profile_code',
        'full_name',
        'display_name',
        'email',
        'phone_number',
        'whatsapp_number',
        'languages_spoken',
        'city',
    )

    list_editable = (
        'status',
        'is_public',
        'is_featured',
    )

    readonly_fields = (
        'profile_code',
        'age',
        'created_at',
        'updated_at',
        'resume_file_link',
    )

    fieldsets = (
        ('Account Link', {
            'fields': (
                'user',
                'profile_code',
            )
        }),

        ('Basic Profile Information', {
            'fields': (
                'service_type',
                'full_name',
                'display_name',
                'gender',
                'date_of_birth',
                'age',
            )
        }),

        ('Location Information', {
            'fields': (
                'city',
                'district',
                'country',
                'address',
            )
        }),

        ('Profile Photo & Documents', {
            'fields': (
                'profile_photo',
                'resume_or_bio_data',
                'resume_file_link',
            )
        }),

        ('Public Profile Content', {
            'fields': (
                'short_intro',
                'about_me',
                'languages_spoken',
                'experience_summary',
                'skills',
                'education',
            )
        }),

        ('Contact Information', {
            'fields': (
                'phone_number',
                'whatsapp_number',
                'email',
            )
        }),

        ('Service Readiness', {
            'fields': (
                'has_license',
                'passport_ready',
                'willing_to_relocate',
                'live_in_available',
                'part_time_available',
                'full_time_available',
            )
        }),

        ('Visibility & Approval', {
            'fields': (
                'status',
                'is_public',
                'is_featured',
                'agreed_to_terms',
            )
        }),

        ('System Information', {
            'fields': (
                'created_at',
                'updated_at',
            )
        }),
    )

    def resume_uploaded(self, obj):
        return bool(obj.resume_or_bio_data)

    resume_uploaded.boolean = True
    resume_uploaded.short_description = "Resume/Biodata"

    def resume_file_link(self, obj):
        if obj.resume_or_bio_data:
            return format_html(
                '<a href="{}" target="_blank" class="button">View / Download Resume or Biodata</a>',
                obj.resume_or_bio_data.url
            )
        return "No resume or biodata uploaded."

    resume_file_link.short_description = "Resume / Biodata File"

@admin.register(ServicePartnerInterest)
class ServicePartnerInterestAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'foreigner',
        'partner',
        'status',
        'created_at',
    )
    list_editable = ('status',)
    list_filter = ('status', 'partner__service_type', 'created_at')
    search_fields = (
        'foreigner__user__username',
        'partner__full_name',
        'partner__display_name',
        'partner__profile_code',
        'message',
    )
    readonly_fields = ('created_at',)
    autocomplete_fields = ('foreigner', 'partner')
    ordering = ('-created_at',)


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