from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from .models import MachineModel, SparePart, ServiceIntervention, TechnicianProfile

# Define an inline admin descriptor for TechnicianProfile model
# which acts a bit like a singleton
class TechnicianProfileInline(admin.StackedInline):
    model = TechnicianProfile
    can_delete = False
    verbose_name_plural = 'Perfil de Técnico'

# Define a new User admin
class UserAdmin(BaseUserAdmin):
    inlines = (TechnicianProfileInline,)

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

@admin.register(TechnicianProfile)
class TechnicianProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'is_technician')
    list_filter = ('is_technician',)
    search_fields = ('user__username', 'user__first_name', 'user__last_name')


@admin.register(MachineModel)
class MachineModelAdmin(admin.ModelAdmin):
    list_display = ('name', 'brand')
    search_fields = ('name', 'brand')

@admin.register(SparePart)
class SparePartAdmin(admin.ModelAdmin):
    list_display = ('name', 'reference_code', 'stock_quantity')
    search_fields = ('name', 'reference_code')
    list_filter = ('stock_quantity',)

@admin.register(ServiceIntervention)
class ServiceInterventionAdmin(admin.ModelAdmin):
    list_display = ('machine', 'technician', 'status', 'created_at')
    list_filter = ('status', 'technician', 'created_at')
    search_fields = ('machine__name', 'description')
    autocomplete_fields = ('machine', 'technician')