from django.contrib import admin
from .models import Institute, Client, ExcelEntry, SecondTableEntry, Betriebskosten

# Register Institute model
admin.site.register(Institute)

@admin.register(Client)
class ClientAdmin(admin.ModelAdmin):
    list_display = ('name', 'institute', 'address')  # Added institute here
    search_fields = ('name', 'institute__name')  # Added institute search
    
    # FIX: Include 'institute' in the fields list
    fields = ['name', 'institute', 'address']  # Added 'institute' here

@admin.register(SecondTableEntry)
class SecondTableEntryAdmin(admin.ModelAdmin):
    list_display = ('id', 'client', 'date', 'is_warm', 'lhe_output_short', 'notes_preview')
    list_display_links = ('id', 'client')
    list_editable = ('is_warm',)
    list_filter = ('is_warm', 'client')
    search_fields = ('client__name', 'notes')
    date_hierarchy = 'date'
    ordering = ('-date',)
    
    fieldsets = (
        (None, {
            'fields': ('client', 'date')
        }),
        ('LHe Data', {
            'fields': ('is_warm', 'lhe_delivery', 'lhe_output'),
            'description': 'Enter all liquid helium measurements'
        }),
        ('Additional Info', {
            'fields': ('notes',),
            'classes': ('collapse',)
        })
    )
    
    def lhe_output_short(self, obj):
        return f"{obj.lhe_output} L" if obj.lhe_output else "-"
    lhe_output_short.short_description = 'Output'
    
    def notes_preview(self, obj):
        return obj.notes[:30] + '...' if obj.notes else ""
    notes_preview.short_description = 'Notes Preview'

# Register other models
admin.site.register(ExcelEntry)
admin.site.register(Betriebskosten)