from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from .models import ChronographAnalysis, Shot, ShotGroup


class ShotInline(admin.TabularInline):
    model = Shot
    extra = 0
    readonly_fields = ('shot_number',)
    fields = ('shot_number', 'velocity_fps', 'notes')


class ShotGroupInline(admin.TabularInline):
    model = ShotGroup
    extra = 0
    show_change_link = True
    fields = ('label', 'distance_m', 'order', 'ammo_batch', 'notes')
    raw_id_fields = ('ammo_batch',)


@admin.register(ChronographAnalysis)
class ChronographAnalysisAdmin(admin.ModelAdmin):
    list_display = ('name', 'user', 'date', 'created_at')
    search_fields = ('name', 'user__email', 'notes')
    readonly_fields = ('created_at', 'updated_at')
    raw_id_fields = ('user',)
    inlines = [ShotGroupInline]


@admin.register(ShotGroup)
class ShotGroupAdmin(admin.ModelAdmin):
    list_display = ('label', 'analysis', 'distance_m', 'order', 'ammo_batch')
    search_fields = ('label', 'analysis__name')
    raw_id_fields = ('analysis', 'ammo_batch')
    inlines = [ShotInline]


@admin.register(Shot)
class ShotAdmin(admin.ModelAdmin):
    list_display  = ('shot_number', 'group', 'velocity_fps', 'notes')
    search_fields = ('group__label', 'group__analysis__name')
    readonly_fields = ('shot_number',)
    raw_id_fields = ('group',)
    ordering      = ('group', 'shot_number')
