from django.contrib import admin

from .models import Business, BusinessMembership, Customer, Feedback, Job, JobMedia, ProofCard, ReviewRequest


class JobMediaInline(admin.TabularInline):
    model = JobMedia
    extra = 0


@admin.register(Business)
class BusinessAdmin(admin.ModelAdmin):
    list_display = ('name', 'industry', 'primary_city', 'primary_state', 'is_active')
    list_filter = ('industry', 'is_active')
    search_fields = ('name', 'slug', 'primary_city')
    prepopulated_fields = {'slug': ('name',)}


@admin.register(BusinessMembership)
class BusinessMembershipAdmin(admin.ModelAdmin):
    list_display = ('user', 'business', 'role', 'created_at')
    list_filter = ('role', 'business')
    search_fields = ('user__email', 'user__first_name', 'user__last_name', 'business__name')


@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
    list_display = ('full_name', 'business', 'city', 'state', 'email', 'phone')
    list_filter = ('business', 'state')
    search_fields = ('full_name', 'email', 'phone')


@admin.register(Job)
class JobAdmin(admin.ModelAdmin):
    list_display = ('service_type', 'business', 'customer', 'city', 'state', 'completed_at', 'status')
    list_filter = ('business', 'status', 'state', 'completed_at')
    search_fields = ('service_type', 'customer__full_name', 'technician_name')
    inlines = [JobMediaInline]


@admin.register(ReviewRequest)
class ReviewRequestAdmin(admin.ModelAdmin):
    list_display = ('job', 'channel', 'status', 'sent_at', 'reviewed_at')
    list_filter = ('channel', 'status')
    search_fields = ('job__customer__full_name', 'job__service_type')


@admin.register(Feedback)
class FeedbackAdmin(admin.ModelAdmin):
    list_display = ('review_request', 'experience', 'rating', 'follow_up_required', 'is_public_approved', 'created_at')
    list_filter = ('experience', 'follow_up_required', 'is_public_approved')
    search_fields = ('review_request__job__customer__full_name', 'testimonial')


@admin.register(ProofCard)
class ProofCardAdmin(admin.ModelAdmin):
    list_display = ('job', 'customer_display_name', 'status', 'is_featured', 'rating', 'published_at')
    list_filter = ('status', 'is_featured')
    search_fields = ('customer_display_name', 'job__service_type', 'testimonial_quote')
