from django.contrib import admin
from .models import (
    Fertilizer, FertilizationPlan, FertilizationEntry,
    DeliveryPlan, DeliveryGroup, DeliveryGroupField, DeliveryTrip, DeliveryTripItem,
    SpreadingSession, SpreadingSessionItem,
)


@admin.register(Fertilizer)
class FertilizerAdmin(admin.ModelAdmin):
    list_display = ['name', 'maker', 'capacity_kg', 'nitrogen_pct']


class FertilizationEntryInline(admin.TabularInline):
    model = FertilizationEntry
    extra = 0


@admin.register(FertilizationPlan)
class FertilizationPlanAdmin(admin.ModelAdmin):
    list_display = ['name', 'year', 'variety', 'is_confirmed', 'confirmed_at']
    list_filter = ['year']
    inlines = [FertilizationEntryInline]


class DeliveryGroupFieldInline(admin.TabularInline):
    model = DeliveryGroupField
    extra = 0
    readonly_fields = ['delivery_plan']


class DeliveryGroupInline(admin.TabularInline):
    model = DeliveryGroup
    extra = 0


class DeliveryTripItemInline(admin.TabularInline):
    model = DeliveryTripItem
    extra = 0


class DeliveryTripInline(admin.TabularInline):
    model = DeliveryTrip
    extra = 0


@admin.register(DeliveryPlan)
class DeliveryPlanAdmin(admin.ModelAdmin):
    list_display = ['name', 'year', 'created_at']
    list_filter = ['year']
    inlines = [DeliveryGroupInline, DeliveryTripInline]


@admin.register(DeliveryGroup)
class DeliveryGroupAdmin(admin.ModelAdmin):
    list_display = ['name', 'delivery_plan', 'order']
    inlines = [DeliveryGroupFieldInline]


@admin.register(DeliveryTrip)
class DeliveryTripAdmin(admin.ModelAdmin):
    list_display = ['delivery_plan', 'order', 'name', 'date']
    inlines = [DeliveryTripItemInline]


class SpreadingSessionItemInline(admin.TabularInline):
    model = SpreadingSessionItem
    extra = 0


@admin.register(SpreadingSession)
class SpreadingSessionAdmin(admin.ModelAdmin):
    list_display = ['year', 'date', 'name']
    list_filter = ['year', 'date']
    inlines = [SpreadingSessionItemInline]
